How can I add a file to the last commit in Git? [duplicate]


Sometimes after I did a commit, I found out that I left out a file which should also be included in the commit, but was actually not. I often committed again:

git add the_left_out_file
git commit "include the file which should be added in the last commit"

I think it might not be a good idea to do so. I want to just include the file without adding a commit. Something like this,

git add the_left_out_file
git add_staged_files_to_previous_commit

Is it possible?


Yes, there's a command, git commit --amend, which is used to "fix" the last commit.

In your case, it would be called as:

git add the_left_out_file
git commit --amend --no-edit

The --no-edit flag allows to make an amendment to the commit without changing the commit message.


You should never amend public commits that you already pushed to a public repository, because amend is actually removing the last commit from the history and creating a new commit with the combined changes from that commit and new added when amending.

Getting Git to work with a proxy server - fails with "Request timed out"

How do I make a Git commit in the past?