6

Let's assume i have 3 commits:

Added bar.txt     (3)
Second Commit     (2)
Initial Commit    (1)

How can i change the commit message from (2) by using its SHA ID? The commit was not pushed to the remote repository yet.

I tried: git commit --amend -m "Added foo.txt" 8457931

8457931 are the first 7 numbers from the SHA ID.

Reason why this is not a duplicate: I ask on how to change the commit message by using the SHA ID to point at the commit which i would like to change, unlike in the linked question.

Black
  • 18,150
  • 39
  • 158
  • 271
  • Take a look at [this answer](http://stackoverflow.com/a/180085/558021), you'll have to perform an interactive rebase - this will bring up an interface that will let you edit older commits after which it will "replay" all of those commits and use your new commit messages... – Lix Dec 14 '15 at 14:52
  • 2
    Possible duplicate of [Edit an incorrect commit message in Git](http://stackoverflow.com/questions/179123/edit-an-incorrect-commit-message-in-git) – Lix Dec 14 '15 at 14:53
  • Hmm okay, but this does not show how to amend it by using the SHA ID. – Black Dec 14 '15 at 14:55
  • When you are inside the rebase, you'll be presented with a list of commits including their SHA. You can't access a specific commit without taking into consideration the tree that it is a part of. Once you have your list of commits in the rebase, you'll be able to find your SHA ID. You'll need to initiate the rebase on the commit *before* the one you want to change. – Lix Dec 14 '15 at 14:56
  • Just to clear this up: the fact that there is a suggested duplicate for your question doesn't necessarily mean that you are asking the exact same question but rather than an **answer** on that other question would be enough to answer **this** question. Your edit is not really needed - it would be a good thing to remove it. – Lix Dec 14 '15 at 14:59
  • It said that i have to edit my question to show why it is not a duplicate tho. – Black Dec 14 '15 at 15:01
  • The fact is that in the context of [so] - because the other post **does** contain an answer to your question - it is indeed a duplicate - this doesn't mean that it's a bad question at all.. It also doesn't mean that it needs to be deleted - it only means that an answer has already been provided elsewhere. – Lix Dec 14 '15 at 15:02
  • I can't find the answer for my question on the linked page, can you show me where it is? – Black Dec 14 '15 at 15:03
  • I already linked to it in my first comment. - http://stackoverflow.com/questions/179123/edit-an-incorrect-commit-message-in-git/180085#180085 (it's not the accepted answer - but it **is** your solution) – Lix Dec 14 '15 at 15:03
  • I just searched the whole page for SHA but none of the results answered my question, sry. – Black Dec 14 '15 at 15:06
  • 2
    There is nothing to search - the link I provided points directly to the answer. If you read it - you'll see. The answer uses the variable `$parent_of_flawed_commit` in place of an actual SHA ID. There is no copy paste answer for this - you'll need to understand exactly what you are doing since you are actually intending to change the history of a repository. – Lix Dec 14 '15 at 15:08
  • Thanks to you and the answer of Vasfed i understand now how it works. Thank you! :) – Black Dec 14 '15 at 15:17
  • Just an FYI: the duplicate question [contains the same answer that you've accepted.](http://stackoverflow.com/a/180085/1079354) – Makoto Dec 14 '15 at 15:53
  • No it doesn't, it contains a little part of it, it does not show how to get the parent ID, i edited the accepted answer and added the other necessary part for the solution to it. – Black Dec 15 '15 at 07:14

1 Answers1

9

Do an interactive rebase, it is described in https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History

git rebase -i HEAD~2

Mark all as 'pick'(just retain that commit) or 'reword' for changing message. Note that all these commits will be rewritten, so it's better not to go deeper than origin/HEAD points

EDIT: you need to rebase on parent of the commit in question (note the ~1 after sha)

git rebase --interactive <your_sha>~1

Now a file opens:

pick b35b85c second commit
pick 9cc745b Initial commit

Search the line where your target commit is and change pick to reword:

reword b35b85c second commit
pick 9cc745b Initial commit

Save the file. Now another file opens, delete the first line and replace it with your new commit message. Save the file. Done.

Vasfed
  • 18,013
  • 10
  • 47
  • 53
  • Okay thank you, but this does not answer my question on how to amend the message by using the SHA ID, is it even possible? – Black Dec 14 '15 at 14:54
  • 1
    You cannot change single commit if it is not on top of branch, all commits that point to it have to be rewritten. – Vasfed Dec 14 '15 at 14:56
  • Okay so it is impossible to solve it by just using the SHA ID to point at the commit which message i want to change? – Black Dec 14 '15 at 14:59
  • Get its parent via `git cat-file -p ` and `rebase -i` on it, your commit will be at bottom of commit selection – Vasfed Dec 14 '15 at 15:07
  • Your explanation was more confusing then necessary but now thx to @Lix i understand how it works. Thanks! – Black Dec 14 '15 at 15:18
  • cat-file and grep to get the parent SHA ?? – Andrew C Dec 14 '15 at 17:19
  • @AndrewC why not? Works perfect for me – Black Dec 15 '15 at 07:15
  • @EdwardBlack - because Git provides SHA~1 to do the same, which doesn't fork a new process, and doesn't involve unreliable regex – Andrew C Dec 15 '15 at 16:05
  • For a one-off job - whatever floats you boat, actually I agree `~1` is cleaner – Vasfed Dec 15 '15 at 16:11
  • @Vasfed, how can i make use of this trick? Does it work like this: `git rebase --interactive SHA~1` ? – Black Dec 18 '15 at 11:07