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?
Changing the Last Commit: git commit --amend. The git commit --amend command is a convenient way to modify the most recent commit. It lets you combine staged changes with the previous commit instead of creating an entirely new commit.
To change the most recent commit message, use the git commit --amend command. To change older or multiple commit messages, use git rebase -i HEAD~N .
You can use git rebase --interactive , using the edit command on the commit you want to amend.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With