Categories
git git-commit

Remove file from latest commit

2025

How do I remove a file from the latest commit?

3

3810

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 (the old way):

git reset HEAD path/to/unwanted_file

Note, that since Git 2.23.0 one can (the new way):

git restore --staged path/to/unwanted_file

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

git commit -c ORIG_HEAD  

28

  • 106

    Thanks for this. It’s worth adding that if you have already pushed your earlier (wrong) commit, and now try to git push your fix up to your repo, it will complain Updates were rejected because the tip of your current branch is behind its remote counterpart.. If you’re sure that you want to push them (e.g. it’s your fork) then you could use the -f option to force the push, e.g. git push origin master -f. (Do not do this to an upstream repo that others are fetching from)

    Apr 9, 2013 at 19:47


  • 69

    git reset --soft HEAD^ is my most common undo operation

    – funroll

    May 17, 2013 at 16:51

  • 5

    @PabloFernandez at the top of all of the answers are three tabs which allow you to control the ordering of answers: active, oldest and votes. My guess is yours is set to oldest. Switch it to votes even though the accepted answer will still be at the top, this answer will be second.

    – ahsteele

    Aug 24, 2013 at 17:52


  • 20

    I knew this much about git reset but wanted a way to affect the existing commit “in place”. I just learned about git commit -C. So for me, what I want is your exact recipe with one more step, the “new commit again” spelled out as git commit -C [hash of original HEAD commit from first step].

    – metamatt

    Jan 8, 2014 at 23:56

  • 3

    Remember to do an actual commit, not an amend. If you do, it will be amending the previous commit. – oops

    Jul 22, 2015 at 19:05

387

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!

6

  • 150

    you can also use git rm --cached to keep the files on disk

    Oct 15, 2013 at 18:29

  • 19

    Warning to those browsing this answer: make sure you want to DELETE the file (as in gone gone gone!), not just remove it from the Commit list.

    – SMBiggs

    Jun 1, 2015 at 14:11

  • 11

    To add to what others say (and to make it easier to remember not to do this unless you really want to): The rm in the git command is doing what rm itself does!

    – yo’

    Feb 22, 2016 at 9:41

  • 3

    Note that the files can still be restored, in case you changed your mind, the commit before git commit --amend is still there and can be found for example with git reflog. So it is not as bad as the other comments suggest.

    – Steohan

    Dec 6, 2017 at 15:18


  • 2

    When pushing you will need force push: git push -f if you use --amend.

    Oct 15, 2020 at 8:44

216

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.

9

  • 12

    If you want to actually remove the files from the repo (not the filesystem), rather than merely revert them to a previous version, then instead of step 1 do git rm --cached <file(s)>.

    Oct 21, 2016 at 12:34

  • 4

    Wait you can just move commits around the interactive-rebase file at will?

    May 25, 2017 at 18:50

  • 4

    You totally can, but you may(or may not) get conflicts.

    – Brian

    May 28, 2017 at 3:27

  • 13

    This process could be made a bit easier by adding --fixup=35c23c2 to the git commit command. This will setup the commit automatically as a fixup of the required commit and so you won’t need to specify it in the rebase. Additionally, if you add --autosquash to the git rebase command, git will automatically move your commit to the correct location, so you don’t need to do anything in the interactive rebase – just save the result (which means you don’t even need to -i flag, though I like to use it anyway to make sure everything looks as I expect).

    – Guss

    Jun 21, 2017 at 15:06

  • 2

    didnt work, it just does nothing after doing checkout spcific commit and file

    Mar 28, 2019 at 12:10