How to Remove a File from a Git Commit

Remove a File from a Commit

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:
git rm <unwanted-file>
  • Amend the Commit: Close the commit message editor.
git commit --amend
  • Continue the Rebase:
git rebase --continue

Considerations

  • 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.

With SmartGit

Splitting a commit showing the commit changes in the index
Right-click the commit in the Graph and select Split. This will start an intermediate rebase and put all commit changes in the index.
After unstaging the unwanted file, commit
Remove the file from the index by clicking Unstage. To create the new commit with the file excluded, click Commit.
Confirm that you don’t want to apply all changes
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
Illustration of Smart Gitty, the SmartGit mascot -- a grey cartoon cat with glasses, sitting behind a laptop