programming pointers


git amend scenerios

Sometimes we need to change the commit message of already committed/committed-pushed files. See below some of the scenarios might arise..

Scenerio 1-> Committed but not pushed

$ git commit --amend 

This will open an editor with the commit message. If you are using vi editor edit the commit message and save using !wq: You can check with $git log if the commit message is amended correctly.

Scenerio 2-> Already pushed + most recent commit

It might be the case that if a user has already pushed the changes to git central repository, in this type of scenario we need to first amend the most recent local commit and afterward apply -force push which will forcefully push the changes to the server. In this process, one thing to keep in mind is that if in between any other user who has already synced local copy with the central repository needs to re-pull.

$ git commit --amend

Edit the message in vi and save and exit

$ git push origin <branch_name> --force 

Scenerio 3-> Not pushed + old commit

$ git rebase -i HEAD~X

where X is the number of commits to go back then move to the line of your commit, change pick and edit then change your commit message. e.g.; Go back to previous 2 commits

$ git rebase -i HEAD~2

Note: It’s always good idea to use

$ git log 

to check the commit history to avoid the mistake in rebase.

Use amend

$ git commit --amend 

and finish the rebase with:

$ git rebase --continue

Rebase opened your history and let you pick what to change. With edit, you can change the message. Git moves you to a new branch to let you amend the message.

$ git rebase --continue

puts you back in your previous branch with the changed message.

alternatively, you can choose


instead of edit when rebasing to change the commit directly. Then you can skip the amend and rebase continue. You may check this link from git book for more on this.

Scenerio 4-> Already Pushed + Old Commit
Edit your message with the same 3 steps process as we saw in scenerios 2

rebase -i, commit --amend, rebase --continue

Then force push the commit.

$git push origin <branch_name> --force

Please so remember re-pushing your commit after changing, it will be very likely to prevent others to sync with the repo if they already pulled a copy. You should first check with them.





Copyright © 2007-2023 PRADEEP K. PANT

Made with Jekyll | Source Code | RSS