Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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.

@simono

This comment has been minimized.

Copy link

@simono simono commented Jul 11, 2016

You can choose reword instead of edit when rebasing to change the commit message directly. Then you can skip the amend and rebase continue.

@nepsilon

This comment has been minimized.

Copy link
Owner Author

@nepsilon nepsilon commented Jul 12, 2016

Thanks for the tip! I completely missed that one.

@simono

This comment has been minimized.

Copy link

@simono simono commented Jul 12, 2016

Sure thing. Thanks for the awesome gist 😄

@imlocle

This comment has been minimized.

Copy link

@imlocle imlocle commented Nov 15, 2016

Hey. I'm trying to change the first-commit-message.

I tried to search for it with git rebase -i HEAD~77 but it didn't show up.

When I tried to do anything higher than 77, it states, "fatal: Needed a single revision. invalid upstream HEAD~78"

@shaharao

This comment has been minimized.

Copy link

@shaharao shaharao commented Jan 4, 2018

@imlocle Try recounting your commits. You might not have 77 or more than 77commits.

@shaharao

This comment has been minimized.

Copy link

@shaharao shaharao commented Jan 4, 2018

This gist is really helpful. Thanks!

@shreyas1496

This comment has been minimized.

Copy link

@shreyas1496 shreyas1496 commented Jan 4, 2018

@nepsilon This works like a charm.

@Juddd

This comment has been minimized.

Copy link

@Juddd Juddd commented Jan 12, 2018

How to input you such emoji with text form?

@Juddd

This comment has been minimized.

Copy link

@Juddd Juddd commented Jan 13, 2018

I use your method to change those message I have pushed successfully. But seem git rebase --continue don't work anymore? If I run it, it will put me a information like:

No rebase in progress?

@Juddd

This comment has been minimized.

Copy link

@Juddd Juddd commented Jan 13, 2018

As the reminder, I think reword is more suitable for this target..

@kshitijgorde

This comment has been minimized.

Copy link

@kshitijgorde kshitijgorde commented Feb 20, 2018

Thanks. It was helpful!

@gerome0123

This comment has been minimized.

Copy link

@gerome0123 gerome0123 commented Apr 24, 2018

+1

@superche

This comment has been minimized.

Copy link

@superche superche commented May 18, 2018

Thanks! It's really helpful! 👍

@divivu

This comment has been minimized.

Copy link

@divivu divivu commented May 22, 2018

Thanks for nice tip!

@brahbassim

This comment has been minimized.

Copy link

@brahbassim brahbassim commented May 23, 2018

Very helpfull

@mgurnani

This comment has been minimized.

Copy link

@mgurnani mgurnani commented Jun 24, 2018

Really helpful !!

@lokhandeomkar

This comment has been minimized.

Copy link

@lokhandeomkar lokhandeomkar commented Jul 28, 2018

What will others have to do to be able to sync with the repo if they have already pulled?

@saso008

This comment has been minimized.

Copy link

@saso008 saso008 commented Aug 4, 2018

I have two commit and use the (git rebase -i HEAD~2) for change commit two, and why give me the commit head for change?!
in other word , if (n) commit i don't change the commit (n).

@quannh02

This comment has been minimized.

Copy link

@quannh02 quannh02 commented Oct 8, 2018

Thank it helpful

@vasanthdeveloper

This comment has been minimized.

Copy link

@vasanthdeveloper vasanthdeveloper commented Nov 2, 2018

Thank you 👍

@code0wl

This comment has been minimized.

Copy link

@code0wl code0wl commented Dec 7, 2018

Great gist!
You can also use git push --force-with-lease when force pushing to the branch if you are worried about not overriding other's work.

@ppant

This comment has been minimized.

Copy link

@ppant ppant commented Mar 12, 2019

Very good.. helpful. Thanks

@ggrrll

This comment has been minimized.

Copy link

@ggrrll ggrrll commented Mar 14, 2019

thanks!

@nadieenespecial

This comment has been minimized.

Copy link

@nadieenespecial 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

This comment has been minimized.

Copy link

@ithink20 ithink20 commented May 25, 2019

👍

@matteopallini-migacore

This comment has been minimized.

Copy link

@matteopallini-migacore matteopallini-migacore commented Aug 30, 2019

thanks really handy

@MWhyte

This comment has been minimized.

Copy link

@MWhyte MWhyte commented Nov 3, 2019

Excellent. Thank you

@midoushitongtong

This comment has been minimized.

Copy link

@midoushitongtong midoushitongtong commented Nov 27, 2019

👍 thanks

@csonuryilmaz

This comment has been minimized.

Copy link

@csonuryilmaz csonuryilmaz commented Dec 9, 2019

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

This comment has been minimized.

Copy link

@rishikeshjoshi rishikeshjoshi commented Feb 1, 2020

Thank you.

@19007361

This comment has been minimized.

Copy link

@19007361 19007361 commented Mar 4, 2020

Already pushed + most recent commit:

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

This comment has been minimized.

Copy link

@nerdCopter nerdCopter commented Mar 29, 2020

so there is no way to retain existing commit SHA's for old history?

@leoiamele

This comment has been minimized.

Copy link

@leoiamele leoiamele commented Apr 3, 2020

Thanks! This is really helpful!

@normancarcamo

This comment has been minimized.

Copy link

@normancarcamo normancarcamo commented Apr 8, 2020

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

This comment has been minimized.

Copy link

@fredriccliver fredriccliver commented May 15, 2020

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.