Sometimes, you may want to change the last commit you’ve made.
--amend allows you do that. With that option, you can replace the last commit with a new one. But take care to do that if you’ve pushed it to the remote and there are other members in the repository. In such case, they may have made changes based on your last commit, then remember to notice them to execute
git pull --rebase to avoid troubles after you pushed the new commit .
A more complicated case is before you amend your commit, someone have pushed their commits based on your last commit. In such a case, a simple way is to execute
git pull to get the new changes, and make a new commit in which changes of your last commit can be reverted with
git revert command.
# Pull new changes that have been pushed after your last commit $ git pull # Revert your last commit. Note you may need to fix some conflicts if they occur. # If nothing is wrong, a new commit will be generated automatically. # There will be a chance to write the commit message, or you can leave it as it was. # Examples: # git revert 6d84d27 # git revert HEAD~~ # git revert HEAD~3 $ git revert # Push $ git push
A complicated method is to execute
rebase command to make the subsequent commits being based on the changed new one. It is not discussed here.
Process of changing the last commit
Below is a full process to make sure every thing will be fine in various situations.
# Step 1 # Make sure you are at the tip of the branch (the others have not pushed new commits # based on your last one), notice the other members not to push during the process. # Check whether you are ahead or behind the remote branch $ git branch --vv # If you are not behind the remote branch, go to the next step. # If you are behind, see the second paragraph part of the article. # Step 2 # Make modifications in the working directory # Step 3 # Add the modifications $ git add functions.php # Step 4 # Recommit with --amend option # --amend, replace the tip of the current barnch by creating a new commit $ git commit --amend -m 'new commit message' # Step 5 # If you have not pushed the last commit. $ git push # If the last commit has been pushed to remote, repush it with --force. # --force, force to push to allow overwriting the commits in the remote. # Use --force with care. $ git push --force # Step 6 # Notice others to run "git pull --reabase" if there are other members.
If you want to change the commit for specific needs, such as only changing the commit message or keeping the old commit message, see below.
1. Change the last commit without changing commit message
# Amend commit without changing commit message # --no-edit, use the old commit message without lauching an editor to change it $ git commit --amend --no-edit
2. Change only the commit message in the last commit
# Change the commit message $ git commit --amend -m 'new commit message'
Below is an example to change the author to be “john” and the email to be “email@example.com”. Remember to wrap the email with “ in the first command.
# Change author info in the last commit. # the new author is john, and email is firstname.lastname@example.org # --author="<Author <Author email>" $ git commit --amend --no-edit --author="john <email@example.com>" # Or # First set the author and email for the repo # and then change the commit with the new configured author and email. # --reset-author, renew the author infor in the commit with the configured user info. $ git config user.name john $ git config user.email firstname.lastname@example.org $ git commit --amend --no-edit --reset-author