Remove files from Git commit Remove files from Git commit git git

Remove files from Git commit


I think other answers here are wrong, because this is a question of moving the mistakenly committed files back to the staging area from the previous commit, without cancelling the changes done to them. This can be done like Paritosh Singh suggested:

git reset --soft HEAD^ 

or

git reset --soft HEAD~1

Then reset the unwanted files in order to leave them out from the commit:

git reset HEAD path/to/unwanted_file

Now commit again, you can even re-use the same commit message:

git commit -c ORIG_HEAD  


ATTENTION! If you only want to remove a file from your previous commit, and keep it on disk, read juzzlin's answer just above.

If this is your last commit and you want to completely delete the file from your local and the remote repository, you can:

  1. remove the file git rm <file>
  2. commit with amend flag: git commit --amend

The amend flag tells git to commit again, but "merge" (not in the sense of merging two branches) this commit with the last commit.

As stated in the comments, using git rm here is like using the rm command itself!


Existing answers are all talking about removing the unwanted files from the last commit.

If you want to remove unwanted files from an old commit (even pushed) and don't want to create a new commit, which is unnecessary, because of the action:

1.

Find the commit that you want the file to conform to.

git checkout <commit_id> <path_to_file>

you can do this multiple times if you want to remove many files.

2.

git commit -am "remove unwanted files"

3.

Find the commit_id of the commit on which the files were added mistakenly, let's say "35c23c2" here

git rebase 35c23c2~1 -i  // notice: "~1" is necessary

This command opens the editor according to your settings. The default one is vim.

Move the last commit, which should be "remove unwanted files", to the next line of the incorrect commit("35c23c2" in our case), and set the command as fixup:

pick 35c23c2 the first commitfixup 0d78b28 remove unwanted files

You should be good after saving the file.

To finish :

git push -f

If you unfortunately get conflicts, you have to solve them manually.