Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

Wednesday, October 31, 2018

Remove one file from git commit to a remote branch

Ooops, I did it again. Commited and pushed one file too much.
? git push --set-upstream origin bugfix/JRASERVER-65811
What now? How to remove the file from a public commit?

Let's first go back to the base branch.
? git checkout -
Switched to branch 'develop'
Your branch is up to date with 'origin/develop'.
I assume I may have some changes in my local copy so first need to clean it. I am going to delete my local branch.
? git branch -D bugfix/JRASERVER-65811
Deleted branch bugfix/JRASERVER-65811 (was 5e17f72f).
Next step is to synchronize with remote repository and fetch the branch again.
? git pull
remote: Enumerating objects: 119, done.
remote: Counting objects: 100% (119/119), done.
remote: Compressing objects: 100% (119/119), done.
remote: Total 119 (delta 41), reused 0 (delta 0)
Receiving objects: 100% (119/119), 43.63 KiB | 1.82 MiB/s, done.
Resolving deltas: 100% (41/41), done.
From gitlab:project/frontend
   736ac14a..ea0ba57e  bugfix/JRASERVER-65811-allopenissues -> origin/bugfix/JRASERVER-65811-allopenissues
Already up to date.
I can now switch to the branch back.
? git checkout bugfix/JRASERVER-65811
Switched to a new branch 'bugfix/JRASERVER-65811'
Branch 'bugfix/JRASERVER-65811' set up to track remote branch 'bugfix/JRASERVER-65811' from 'origin'.
Time to bring the last commit into staging.
? git reset --soft HEAD^
I can see this was done by showing status.
? git status
On branch bugfix/JRASERVER-65811
Your branch is behind 'origin/bugfix/JRASERVER-65811' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)

Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        new file:   src/app/lktree/lktree.component.spec.ts
        modified:   src/app/modules/spaces/data/space-datasource.ts
        modified:   src/app/modules/spaces/document-details/document-details.component.html
        modified:   src/app/modules/spaces/spaces-tree/spaces-tree.component.ts
        modified:   src/app/modules/user-context/data/user-context-datasource.ts
The first of files is the one I want to extract and unstage from to working copy. This simply undo git add.
? git reset src/app/lktree/lktree.component.spec.ts
Yeah! My changes are now in state I wanted!
? git status
On branch bugfix/JRASERVER-65811
Your branch is behind 'origin/bugfix/JRASERVER-65811' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)

Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        modified:   src/app/modules/spaces/data/space-datasource.ts
        modified:   src/app/modules/spaces/document-details/document-details.component.html
        modified:   src/app/modules/spaces/spaces-tree/spaces-tree.component.ts
        modified:   src/app/modules/user-context/data/user-context-datasource.ts

Untracked files:
  (use "git add ..." to include in what will be committed)

        src/app/lktree/
I can commit again - this time the right files.
? git commit src/app/modules/spaces/ src/app/modules/user-context/data/user-context-datasource.ts -m "JRASERVER-65811: allopenissues"
[bugfix/JRASERVER-65811 80002b42] JRASERVER-65811: allopenissues
 4 files changed, 60 insertions(+), 25 deletions(-)
Last commit is to overwrite the remote branch. Do not do it at home ;)
? git push --force

Saturday, April 2, 2016

massive directory name case change

Some time ago I had a disk failure. One project code was affected. I managed to restore almost all data, but result was often a file with name changed to upper case. As this happened to a windows machine, I could probably live with that, but it turned out package name in java is case sensitive, and Idea was not able to do quick fix as quickly as I would expect. Manual change is not an option for a developer.
So let's automate that. Solution would be to recursively visit each directory and in case its case is wrong - rename. You can have that in java, I would rather be happy to have that in script, however I am not sure whether scripting cmd or bash port on windows will actually handle that rename sanely.
Fortunately there is nice tool in python that accomplishes the same goal - os.walk. Usage is trivial and script is portable to any platform. File system paths can be also abstracted easily with os.path.join. There is even a method in python string that checks case of whole content - so you will not write any extra loop iterating over characters. Python handles nicely filtering in list comprehensions - so it is possible to use the condition in for statement.
from os import walk, rename
from os.path import join

for (dirpath, dirnames, filenames) in walk( join('src', 'main', 'java') ):
  for d in [f for f in dirnames if f.isupper()]:
       rename(join(dirpath, d), join(dirpath, d.lower()))
As you can see some APIs may be even more developer friendly than what is in Java standard. Now came the difficult part - for some reason git was unable to recognize character case change in filenames. It happens that some default on windows is aligned with os approach to case sensitivity. Making git see the difference is changing its configuration:
git config core.ignorecase false