Is there a way to edit a commit message on GitHub?

135

50

Is there a way to edit a commit message after committing and pushing to GitHub? I see that there is a 'add a note' as well as inline commenting, but no actual editing of a commit message. There is also 'amend commit' in git extensions but that doesn't edit the existing message.

Matthew Peters

Posted 2014-05-09T16:48:06.750

Reputation: 1 718

You can try reverting the commit (see some options in this SO question: https://stackoverflow.com/questions/4114095/revert-to-previous-git-commit) - only make sure you first back up any code changes so you won't lose those for the sake of a comment!

– Traveling Tech Guy – 2014-05-09T17:25:24.503

1

See also How do I edit an incorrect commit message in Git? on Stack Overflow.

– Arjan – 2014-05-10T10:38:38.577

Answers

189

  1. git rebase -i <commit hash you want to change>^

    This will open your default editor (usually ) with a list of commits and actions for each one. By default, the action is pick.

  2. For any commit you wish to change the message, change pick to reword.

  3. Save and quit (in vi: :wq).

  4. For each such commit, you'll get an editor to edit the commit message. Change it as you see fit, save and quit.

    Once you're done editing all the commit messages, you'll return to the command prompt, and have a new tree with the updated messages.

  5. You can now upload them to github by using git push origin --force.

If you just need to fix your last commit, you can replace steps 1-4 with git commit --amend.

Mureinik

Posted 2014-05-09T16:48:06.750

Reputation: 3 521

4It does not seem that you can specify <commit hash you want to change>, rather you need to specify the hash of the commit preceding the one you want to change or use the HEAD~x where x is the number of commits from HEAD where the item you wish to change resides. – ssc327 – 2018-12-18T21:32:52.813

3@ssc327 Note that I a ^ there - I indeed suggested rebasing on the parent of the commit you want to change. – Mureinik – 2018-12-19T05:16:06.377

2@Murenik you are correct, I somehow missed seeing the ^ – ssc327 – 2018-12-19T11:41:23.983

When I add ^ in Window's terminal, it asks me "more?". What am I doing wrong? – deadfish – 2019-01-14T11:58:03.933

@deadfish ^ is the line continuation character in Windows' terminal, see this. However this works in the git-bash terminal on Windows (that is, mintty).

– None – 2019-01-23T09:03:39.447

This feels like cheating. Glad it's possible. – wordsforthewise – 2019-02-07T00:00:09.030

1@deadfish Using the Windows command line, you must type ^^ to end the command with a literal ^ e.g.: git rebase -i 2c747b32^^ – Wyck – 2019-02-15T00:59:53.270

Can you do this in a gui like gitextensions? – Matthew Peters – 2014-05-15T14:57:48.843

3@MatthewPeters I assume there should be a way, but I don't know - I use the commandline directly. – Mureinik – 2014-05-15T15:13:06.070

35

In Intellij Idea you can do it so easy.

  1. Open Version Control (History)
  2. Select log tab
  3. Select commit to change comment
  4. press F2 (Mac fn + F2), and update your commit message

fedrbodr

Posted 2014-05-09T16:48:06.750

Reputation: 459

1Not working if you already pushed to remote. – paynd – 2018-11-05T10:45:15.293

8You've got to execute git push origin --force afterwards as suggested in @Mureinik's answer. – Dan Macák – 2018-11-15T08:21:37.550

works perfect with android studio as well. – Rat-a-tat-a-tat Ratatouille – 2019-01-09T16:57:30.553

1The "reword" option is disabled if the commit has been pushed already. – huyz – 2019-01-10T12:43:25.933

The "reword" option is worked fine even you already commit and push, you just need press f2 then commit and push, then again press f2 and edit the message, then you need force push, it helps me edit pushed commit. – fedrbodr – 2019-01-14T09:28:30.243

The "reword" option is greyed out when the commit has been pushed to the remote repo. The F2 key does nothing. Tested with Intellij IDEA 2019.1 ultimate. – jplandrain – 2019-04-11T09:01:39.837

1To do it with Intellij IDEA for a commit that's been pushed, you must start with an interactive rebase first (like you would do from the command line of Git). In order to do the rebase, right click on your project -> "Git" menu item -> "Repository" -> "rebase..." (last menu item). Insert the SHA of the commit before the one you want to modify in the "Onto" field and click "Rebase". You will then get the interactive rebase prompt. Select "reword" in the action dropbox next to the commit(s) you want to modify and click the "Start rebasing" button (continued in next comment) – jplandrain – 2019-04-11T11:52:30.263

1(continued) You will then be presented a text prompt for every commit you want to modify. After the log messages are modified, you can apply further modifications (note that now the "reword" option is not greyed out anymore). When you have finished, you can then force push your modifications in order to conclude the interactive rebase. The whole process is actually exactly the same as in the answer of @Mureinik who's doing it from the command line instead. – jplandrain – 2019-04-11T11:52:51.540

Note: in IntelliJ, by default force push is disabled on protected branches, and "master" is the default protected branch. Check under "File" -> "Settings..." -> "Version Control" -> "Git" -> "Protected branches". – jplandrain – 2019-04-11T12:05:45.690

3

Premise:

if your git-graph looks like ...

O   target-commit that you want to change its message [df9c192]
|
O   parent-commit [b7ec061]
|
O

(df9c192 and b7ec061 are the commit hashes of target-commit and parent-commit, separately)

Solution:

you can just type the following instructions...

git reset --soft b7ec061
git commit -m "your_new_description"
git push -f

Explanation:

  1. git reset --soft b7ec061 will keep your changes of files and reset to parent-commit (i.e. b7ec061)
  2. git commit -m "..." will locally create a new commit
  3. git push -f will push your new commit to the server and replace the old one (i.e. df9c192)

Alumi Lu

Posted 2014-05-09T16:48:06.750

Reputation: 31

1It changes only my last commit message. what if i want to change before last commit message? – Jigar Bhatt – 2019-11-15T06:10:05.923

you are right! this method only works for the last commit message – Alumi Lu – 2020-01-15T07:27:47.507

2

Another option is to create an additional "errata commit" (and push) which references the commit object that contains the error -- the new errata commit also provides the correction. An errata commit is a commit with no substantive code changes but an important commit message -- for example, add one space character to your readme file and commit that change with the important commit message, or use the git option --allow-empty. It's certainly easier and safer than rebasing, it doesn't modify true history, and it keeps the branch tree clean (using amend is also a good choice if you are correcting the most recent commit, but an errata commit may be a good choice for older commits). This type of thing so rarely happens that simply documenting the mistake is good enough. In the future, if you need to search through a git log for a feature keyword, the original (erroneous) commit may not appear because the wrong keyword was used in that original commit (the original typo) -- however, the keyword will appear in the errata commit which will then point you to the original commit that had the typo. Here's an example:

$ git log
commit 0c28141c68adae276840f17ccd4766542c33cf1d
Author: First Last 
Date:   Wed Aug 8 15:55:52 2018 -0600

    Errata commit:
    This commit has no substantive code change.
    THis commit is provided only to document a correction to a previous commit message.
    This pertains to commit object e083a7abd8deb5776cb304fa13731a4182a24be1
    Original incorrect commit message:
        Changed background color to red
    Correction (*change highlighted*):
        Changed background color to *blue*

commit 032d0ff0601bff79bdef3c6f0a02ebfa061c4ad4
Author: First Last 
Date:   Wed Aug 8 15:43:16 2018 -0600

    Some interim commit message

commit e083a7abd8deb5776cb304fa13731a4182a24be1
Author: First Last 
Date:   Wed Aug 8 13:31:32 2018 -0600

    Changed background color to red

rob_7cc

Posted 2014-05-09T16:48:06.750

Reputation: 131

For sure it is safe, but a lot of text to read. I'd prefer rewriting the history :) – pkalinow – 2019-05-09T10:53:48.240

0

Answer of @Mureinik is good but not understandable by newbie.

First method:

  1. If you only want to edit latest commit message, then you only need git commit --amend, you would see:
<your existing commit mesage foo bar> 

# Please enter the commit message fir your changes. Lines starting
# with # will be ignored, and an empty message aborts the commit.
#
# Date: Sat Aug 24 17:56:16 2019 +0800
#
# On branch is up to date with 'origin/master'.
#
# changes to be committed:
#       modified:   foo.py
#
  1. As you can see, commit message on top without any prefix of command such as pick, this is already the edit page and you can direct edit the top message and save&quit, e.g.:
<your new correction commit message> 

# Please enter the commit message for your changes. Lines starting
....
  1. Then do git push -u origin master --force or <how you push normally> --force. The key here is --force.

Second method:

  1. You can see the commit hash by git log or extract from the repository url, example in my case is 881129d771219cfa29e6f6c2205851a2994a8835

  2. Then you can do git rebase --interactive 881129d771219cfa29e6f6c2205851a2994a8835 or git rebase -i HEAD^ (if the latest)

  3. You would see:

pick <commit hash> <your current commit message>

# Rebase 8db7e8b..fa20af3 onto 8db7e8b
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#  d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
  1. But if you see noop then you are probably typing wrong, e.g. if you do git rebase -i 881129d771219cfa29e6f6c2205851a2994a88 which missing ^ at the end, you better quit the editor without save and figure out the reason:
noop

# Rebase 8db7e8b..fa20af3 onto 8db7e8b
...
  1. If no noop issue, then simply change the word pick to reword , other just remains (you don't edit commit message at this point), e.g:
reword <commit hash> <your current commit message>

# Rebase 8db7e8b..fa20af3 onto 8db7e8b
#
# Commands:
#  p, pick = use commit
...
  1. Save&quit will see the edit page similar to method #1:
<your existing commit mesage foo bar> 

# Please enter the commit message fir your changes. Lines starting
# with # will be ignored, and an empty message aborts the commit.
#
# Date: Sat Aug 24 17:56:16 2019 +0800
#
# interactive rebase in progress; onto b057371
# Last command done (1 command done):
#    reword d996ffb <existing commit message foo bar>
# No commands remaining.
# You are currently editing a commit while rebasing branch 'master' on 'b057371'.
#
# changes to be committed:
#       modified:   foo.py
#
  1. Edit the message on top, same like method #1 and save&quit, e.g:
<your new correction commit message> 

# Please enter the commit message for your changes. Lines starting
....
  1. Again, same like method #1, do git push -u origin master --force or <how you push normally> --force. The key here is --force.

For more info please read the doc.

林果皞

Posted 2014-05-09T16:48:06.750

Reputation: 365