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

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSeareneView Question on Stackoverflow
Solution 1 - GitKonrad 'Zegis'View Answer on Stackoverflow
Solution 2 - GitMuhammed Imran HussainView Answer on Stackoverflow