At some point you’ll find yourself in a situation where you need edit a commit message. That commit might already be pushed or not, be the most recent or burried below 10 other commits, but fear not, git has your back 🙂.
git commit --amend
This will open your $EDITOR
and let you change the message. Continue with your usual git push origin master
.
git commit --amend
git push origin master --force
We edit the message like just above. But need to --force
the push to update the remote history.
git rebase -i HEAD~X
# X is the number of commits to go back
# Move to the line of your commit, change pick into edit,
# then change your commit message:
git commit --amend
# Finish the rebase with:
git rebase --continue
Rebase opened your history and let you pick what to change. With edit you tell you want to 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 message changed.
Edit your message with the same 3 steps process as above (rebase -i
, commit --amend
, rebase --continue
).
Then force push the commit:
git push origin master --force
If you add the
--preserve-merges
option (or its synonym,-p
) to thegit rebase -i
command, then git will try to preserve the merges when rebasing, rather than linearizing the history.This one worked for me because on GitLab we use "Merge commit" as merge method in which "every merge creates a merge commit".