Skip to content

Instantly share code, notes, and snippets.

@git2358
Forked from nepsilon/git-change-commit-messages.md
Last active October 18, 2018 11:46
Show Gist options
  • Select an option

  • Save git2358/9625235ea9e5994b453e205db230ada3 to your computer and use it in GitHub Desktop.

Select an option

Save git2358/9625235ea9e5994b453e205db230ada3 to your computer and use it in GitHub Desktop.
How to change your commit messages in Git? — First published in fullweb.io issue #55

my example:

  1. first do the amended commit message,
  2. in this case, we add the new title and the description,
  3. protip! we can also add new lines using the \n with bash shell $'...'
git commit --amend -m "title" -m $'Description\nNewline\n\nNewline with space'

Not pushed + old commit:

git rebase --continue

Already pushed + most recent commit:

git push origin master --force

refer to the git-change-commit-messages.md forked from nepsilon/git-change-commit-messages.md

How to change your commit messages in Git?

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 🙂.

Not pushed + most recent commit:

git commit --amend

This will open your $EDITOR and let you change the message. Continue with your usual git push origin master.

Already pushed + most recent commit:

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.

⚠️ But! Force pushing your commit after changing it will very likely prevent others to sync with the repo, if they already pulled a copy. You should first check with them.

Not pushed + old commit:

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.

Already pushed + old commit:

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

⚠️ But! Remember re-pushing your commit after changing it will very likely prevent others to sync with the repo, if they already pulled a copy. You should first check with them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment