Wednesday, August 16, 2017

Git: remove a file that no longer exists from a commit

https://stackoverflow.com/questions/30446281/git-remove-a-file-that-no-longer-exists-on-the-system

Note the git amend command will open an editor and the user needs to manually remove the file form the commit list.

1 down vote accepted
You need to remove it from the index before the commit:
git rm --cached FOLDER_NAME
Since you have it already removed from the file system, the folder should have disappeared now.
If you have added the folder in the most recent commit then you can simply issue:
git commit --amend
git push -f REMOTE BRANCH_NAME

If you have added the folder in penultimate commit, things get a little bit more difficult. I suggest to create a backup of the local repo first.
Follow these steps:
# Rebase the second most recent commits
# -i will open an editor
git rebase -i HEAD~2
In the editor replace pick by edit in the line with the second last commit and save the file. You can now change that commit. Issue:
git rm FOLDER_NAME

# This will open an editor again. You can leave the
# commit message unchanged. Simply save the file
git commit --amend

# Finish the rebase action
git rebase --continue
Now you should be done and the folder is removed from that commmit. You can push using
git push REMOTE BRANCH_NAME

No comments:

Post a Comment