Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amend comment message from older commits using SHA ID

Tags:

git

commit

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.

like image 599
Black Avatar asked Dec 14 '15 14:12

Black


People also ask

How do you edit commit message of old commit?

To change the most recent commit message, use the git commit --amend command. To change older or multiple commit messages, use git rebase -i HEAD~N . Don't amend pushed commits as it may potentially cause a lot of problems to your colleagues.

Does amend Change commit ID?

commit message Show activity on this post. Amending a Git commit changes the commit date (which is different from the date you initially see when running git log -- run git log --format=fuller to see the commit date). The commit date is taken into account when creating the commit hash.


1 Answers

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.

like image 54
Vasfed Avatar answered Sep 17 '22 21:09

Vasfed