Skip to content

Instantly share code, notes, and snippets.

@nepsilon
Last active September 7, 2024 11:11
Show Gist options
  • Save nepsilon/156387acf9e1e72d48fa35c4fabef0b4 to your computer and use it in GitHub Desktop.
Save nepsilon/156387acf9e1e72d48fa35c4fabef0b4 to your computer and use it in GitHub Desktop.
How to change your commit messages in Git? — First published in fullweb.io issue #55

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.

@ppant
Copy link

ppant commented Mar 12, 2019

Very good.. helpful. Thanks

@ggrrll
Copy link

ggrrll commented Mar 14, 2019

thanks!

@nadieenespecial
Copy link

nadieenespecial commented Apr 7, 2019

Trying to edit asd2 it give me an error.

commit asd1...
Author: ...
Date:   Sun Apr 7 11:47:46 2019 -0300

    3.32

commit asd2...
Author: ...
Date:   Sun Apr 7 10:52:53 2019 -0300

    X 3.31
git rebase -i HEAD~2
fatal: Needed a single revision
invalid upstream HEAD~2

Solution:
git rebase -i --root

@ithink20
Copy link

👍

@matteopallini-migacore
Copy link

thanks really handy

@MWhyte
Copy link

MWhyte commented Nov 3, 2019

Excellent. Thank you

@midoushitongtong
Copy link

👍 thanks

@csonuryilmaz
Copy link

If you add the --preserve-merges option (or its synonym, -p) to the git 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".

@rishikeshjoshi
Copy link

Thank you.

@19007361
Copy link

19007361 commented Mar 4, 2020

Already pushed + most recent commit:

git commit --amend
git push origin master --force-with-lease

@leoiamele
Copy link

Thanks! This is really helpful!

@normancarcamo
Copy link

What if I want a commit using the hash?
is there any way to do that?

for example:

git commit -c [HASH] -m "new message"

just thinking though.

@fredriccliver
Copy link

👍

@Shivkumar13
Copy link

Hello,
What if I want to change the commit message which I have committed through the GitHub website directly.

So basically I don't have the commits locally, I just changed the code in GitHub UI and opened a PR.

How can I change that commit message? Any ideas?

@omaralamoudi
Copy link

These changes seem to apply to the commit message 1st paragraph or title. What if I wanted to reword the 2nd paragraph?

Thanks!

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