Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to amend a specific commit message in Git?

I am trying to change a commit message in SourceTree but cannot find where the option is. It has not been pushed yet.

How can I amend the message for older commits in SourceTree or command line?

like image 373
bier hier Avatar asked Jun 01 '16 23:06

bier hier


People also ask

Can you edit a git commit message?

You can change the most recent commit message using the git commit --amend command. In Git, the text of the commit message is part of the commit. Changing the commit message will change the commit ID--i.e., the SHA1 checksum that names the commit.

How do I amend a commit in git?

You can use the git commit –amend command to edit a commit message. To do so, use the -m flag and specify a new commit message in quotation marks. This command will replace the single commit log message in your last commit with the one that you state.


3 Answers

There is no feature to do that because how git internally work, a sha1 sealing each commit.

But you could :

  • do a 'amend' if the message is the one of the last commit.

  • do a git rebase -i also named a rebase interactive and choose 'reword' (or 'r') for each commit you want to rewrite the commit message.

  • use git 'notes' to join a new comment next to the existing one (but handle it is not straightforward because you have to push the note explicitely and query them also to see them... )

like image 180
Philippe Avatar answered Sep 28 '22 08:09

Philippe


Suppose the branch is like A-B-C-D-E and you want to amend C. Here is one solution I prefer:

git reset C --hard
#do some changes and add
git commit --amend
git cherry-pick C..E
#or git cherry-pick D E
like image 30
ElpieKay Avatar answered Sep 28 '22 07:09

ElpieKay


  1. Find that commit, using git reflog and then find its sha-id
  2. To go to that commit , use

    git reset --hard sha-id (if you don't want to keep the changes of the current state)

    or,

    use git reset --soft sha-id (if you want to keep the changes)

  3. Now do a commit --amend to the commit.....

  4. Now, check if that the commit , you are amending to, has already been pushed or not,

    if, YES, then do a rebase and push it...

       `git push ` and revert back your head to where it was earlier using its sha-id
    

    If you don't do the above step, your branch will diverge from the remote and you will see that in the git status

    else, just revert back to your commit using its sha-id

like image 40
cafebabe1991 Avatar answered Sep 28 '22 06:09

cafebabe1991