Remove File from a Git Commit
If it is about removing an unintentionally added file from the
latest commit, you can do it by
undoing the last commit. For
older commits, follow the procedure below.
- Start an Interactive Rebase: (mind the trailing
~!) To determine the <commit>-ID of the desired commit, you may use some kind of git log --format=oneline combined with grep.
git rebase --interactive <commit>~
- Edit the Commit: In the first line (it will have the specified
<commit>-ID), change the pick to edit, save and exit the editor.
- Delete the Unwanted File:
- Amend the Commit: Close the commit message editor.
- If the commit is part of your feature branch, which has already been pushed, it will require a force-push.
- Be cautious when attempting to remove a file from a shared branch, as this may cause significant confusion and requires clear communication with all affected users.
Right-click the commit in the Graph and select Split. This will start an intermediate rebase and put all commit changes in the index.
Remove the file from the index by clicking Unstage. To create the new commit with the file excluded, click Commit.
The unstaged file will still be present in the working tree. Delete it by clicking Delete. Click Continue and confirm that you really don’t want to apply all changes by clicking Continue Anyway.
Try these Git operations with SmartGit's intuitive interface
Download SmartGit