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?

2

2 Answers

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.

Warning

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.

6

If you didn't push the update in remote then the simple solution is remove the last local commit using the following command:

git reset HEAD^ 

Then add all files and commit again.

6

You Might Also Like