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.

With Git command line

  • start an interactive rebase git rebase --interactive <commit>~ (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
  • 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 using git rm <unwanted-file>
  • amend-commit using git commit --amend, close the commit message editor
  • continue the rebase using 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

Right-click the commit in the Graph and select Split. This will start an intermediate rebase and put all commit changes in the index.

Splitting a commit showing the 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.

After unstaging the unwanted file, 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.

Confirm that you don't want to apply all changes.