Remove files from Git commit

ID : 479

viewed : 165

Tags : gitgit-commitgit





Top 5 Answer for Remove files from Git commit

vote vote

92

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   
vote vote

87

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!

vote vote

80

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 using ;

git log --graph --decorate --oneline

  1. Checkout that commit using

git checkout <commit_id> <path_to_file>

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

3.

git commit -am "remove unwanted files"

4.

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. If you want to change the global git editor, use;

git config --global core.editor <editor_name>

5.

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 commit fixup 0d78b28 remove unwanted files 

You should be good after saving the file.

6.

To finish :

git push -f

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

vote vote

60

As the accepted answer indicates, you can do this by resetting the entire commit. But this is a rather heavy handed approach.
A cleaner way to do this would be to keep the commit, and simply remove the changed files from it.

git reset HEAD^ -- path/to/file git commit --amend --no-edit 

The git reset will take the file as it was in the previous commit, and stage it in the index. The file in the working directory is untouched.
The git commit will then commit and squash the index into the current commit.

This essentially takes the version of the file that was in the previous commit and adds it to the current commit. This results in no net change, and so the file is effectively removed from the commit.

vote vote

56

If you have not pushed the changes on the server you can use

git reset --soft HEAD~1 

It will reset all the changes and revert to one commit back

If you have pushed your changes then follow steps as answered by @CharlesB

Top 3 video Explaining Remove files from Git commit







Related QUESTION?