Editing the git commit message in GitHub
Asked Answered
C

9

229

Is there any way of online editing the commit message in GitHub.com, after submission?

From the command line, one can do

git commit --amend -m "New commit message"

as correctly suggested in the following question:

Trying git pull and then git push has worked (without any other commit having interfered in the mean time).

But can it be done via the GitHub website?

Crackerbarrel answered 23/5, 2012 at 21:52 Comment(1)
Please see @DanGreen-Leipciger's answer even though it is not the accepted one.Eckhart
A
134

No, this is not directly possible. The hash for every Git commit is also calculated based on the commit message. When you change the commit message, you change the commit hash. If you want to push that commit, you have to force that push (git push -f). But if already someone pulled your old commit and started a work based on that commit, they would have to rebase their work onto your new commit.

Ander answered 23/5, 2012 at 21:56 Comment(5)
I could also do git pull and then git push and it worked. But apparently there is no online way.Crackerbarrel
Still not possible as of April 5th, 2016Iverson
And i guess it won't be high priority, since you would change the commit history of an already published branch, which you should never do (except if you are 200% sure, that nobody else has this branch on his local machine).Ander
This is only half-true. It's true, but only with assumptions. The answer would be better if it were combined with that from @DanGreen (below). It isn't "directly possible", and certainly it comes with forewarnings (ie: "you should 'never' do this... [except when you are sure you want to]") -- but simply saying this isn't possible, when it technically is possible, is an incomplete answer.Label
The OP asked, if it is possible to do it online on the Github website. AFAIK this is still not possible. So my answer is valid, and the answer of Dan Green handles only the way how to do it locally, not on Githubs website.Ander
R
184

GitHub's instructions for doing this:

  1. On the command line, navigate to the repository that contains the commit you want to amend.
  2. Type git commit --amend and press Enter.
  3. In your text editor, edit the commit message and save the commit.
  4. Use the git push --force origin example-branch command to force push over the old commit.

Source: https://help.github.com/articles/changing-a-commit-message/

Responsible answered 6/1, 2017 at 20:3 Comment(11)
What does "navigate to the repo" mean? For me, a commit is in a branch, along with other commits. And now I've pushed everything so my working area is clean. How do I get there to change the message? Sorry - I'm a newbie with this.Manufactory
It means to go the folder in a *nix environment. I.e. if you repo is called "bar" and it lives in a directory called "foo" which lives on the root of your machine, open up a terminal and type cd /foo/bar. You are now ready for step 2Responsible
BE CAREFUL! The difference between using < git commit --amend > & < Enter >, and < git commit --amend -m "new commit message" >, is that in the 1st case you're editing your commit message in a text editor & in the 2nd your replacing it with the "new commit message". If you force the push this will replace the commit on your remote. This does NOT solve the problem that if other people on your team have pulled the previous commit you now have different histories (including different commits) on different machines. If you know no one has pulled your commit this is safe. Read source Dan postedAurelie
git commit --amend has always existed. Also you're getting confused between git (which is an open source command line tool) and Github (which is a company providing an online source hosting).Peristyle
The OP asked about GitHub, and specifically about after the commit has been pushed to their servers. Those are GitHub's instructions for doing what the OP asked about.Responsible
@DanGreen-Leipciger - yeah, but this is not done on the website itself, it is done using command line. I reckon PNS wanted to do that from within the web interface.Prowess
The OP asked how to do it with GitHub specifically, this is how to do it with GitHub specifically.Responsible
so we can only amend the last commit if I understand well ?Refund
Correct, amend is updating the previous commit with whatever you have staged as well as a new message. Should you want to change an old commit message you can do an interactive rebase and mark the commit to be changed with an r. You will be prompted to change it on the next screenResponsible
How to save the edit ? What do you mean by "save the commit"? Where is the command?Clapper
This just dropped if someone wants a GUI method of doing this. retcon.appResponsible
A
134

No, this is not directly possible. The hash for every Git commit is also calculated based on the commit message. When you change the commit message, you change the commit hash. If you want to push that commit, you have to force that push (git push -f). But if already someone pulled your old commit and started a work based on that commit, they would have to rebase their work onto your new commit.

Ander answered 23/5, 2012 at 21:56 Comment(5)
I could also do git pull and then git push and it worked. But apparently there is no online way.Crackerbarrel
Still not possible as of April 5th, 2016Iverson
And i guess it won't be high priority, since you would change the commit history of an already published branch, which you should never do (except if you are 200% sure, that nobody else has this branch on his local machine).Ander
This is only half-true. It's true, but only with assumptions. The answer would be better if it were combined with that from @DanGreen (below). It isn't "directly possible", and certainly it comes with forewarnings (ie: "you should 'never' do this... [except when you are sure you want to]") -- but simply saying this isn't possible, when it technically is possible, is an incomplete answer.Label
The OP asked, if it is possible to do it online on the Github website. AFAIK this is still not possible. So my answer is valid, and the answer of Dan Green handles only the way how to do it locally, not on Githubs website.Ander
K
32

You need to git push -f assuming that nobody has pulled the other commit before. Beware, you're changing history.

Keek answered 23/5, 2012 at 21:55 Comment(0)
C
6

For intellij users: If you want to make changes in interactive way for past commits, which are not pushed follow below steps in Intellij:

  • Select Version Control
  • Select Log
  • Right click the commit for which you want to amend comment
  • Click reword
  • Done

Hope it helps

Conga answered 13/8, 2019 at 13:41 Comment(1)
In JetBrains PHPStorm in 2022 it is slightly differently worded (Git Menu -> Show Git Log -> Right click commit -> Edit Commit Message), but basically the same. Worked a treat, thanks!Chrysolite
T
3

No, because the commit message is related with the commit SHA / hash, and if we change it the commit SHA is also changed. The way I used is to create a comment on that commit. I can't think the other way.

Tableau answered 15/4, 2013 at 9:13 Comment(0)
V
2

For Android Studio / intellij users:

  • Select Version Control
  • Select Log
  • Right click the commit for which you want to rename
  • Click Edit Commit Message
  • Write your commit message
  • Done
Vernice answered 22/11, 2020 at 6:56 Comment(0)
W
2

I was asked to amend a patch commit message that I had submitted on github, (and ended up here.) This is what I did to get the job done.

git clone [email protected]:YOURNAME/EXAMPLE.git; cd EXAMPLE; git fetch --all; git pull --all
git checkout -b patch-2 origin/patch-2 # create local patch-2 branch
git commit --amend # update the commit message
git push -f

This works for the last commit on a branch. If the commit in question is deeper you will need to do something more complicated. (I don't know if the fetch and pull are needed, but I just past in that line while I work on something else to save time.)

Waits answered 8/4, 2021 at 15:18 Comment(0)
J
1

I was facing the same problem.

See in your github for a particular branch and you will come to know the commit id of the very first commit in that branch. do a rebase to that:

git rebase -i <Commit SHA of first commit>

an editor will open up. Do a track of your commits from github UI and open editor and change the messages.

Jacie answered 15/8, 2016 at 23:36 Comment(0)
P
1

For Visual Studio users:

You are able to modify the commit message from the commit tab by clicking Unpushed Commit (1) and the View Outgoing/Incoming option (2). Then, once comment is modified (3), 'Amend Message' option (4) is enabled and changes are performed automatically in the commit.

enter image description here

Tested on VS 2019

Phasis answered 9/2, 2022 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.