Search code examples
gitgit-commit

Remove file from latest commit


How do I remove a file from the latest commit?


Solution

  • 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  
    

    EDIT: The easiest way to do this is to use e.g. git gui. Just select Commit => Amend Last Commit and simply uncheck the desired file from the commit and click Commit.