All How-Tos
Intermediate

How to Remove a File from a Git Commit

Learn how to remove files that were unintentionally added to a Git 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.