Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: change HEAD commit's message without touching the index

I know that I can use git commit --amend --file=path-to-my-new-message but this will amend staged changes, too. Of course, I could stash and later apply&drop the stash, but is there a quicker solution to change the HEAD commit message without committing the staged changes (and user interaction)?

like image 822
Mot Avatar asked Dec 27 '11 16:12

Mot


2 Answers

You can write a new commit message to a file (say msg.txt) and use git commit-tree, e.g.

new_head=$(git commit-tree HEAD^{tree} -p HEAD^ <msg.txt)

git reset --soft $new_head

This assumes that the commit you are amending has one parent, if not you need to supply further -p HEAD^2 -p HEAD^3 ....

It's a bit ugly and low level. You might find it easier to stash your changes and use a straight "amend".

git stash
git commit --amend
git stash pop --index

As @Jefromi suggests, you could also use a temporary index file for the amend operation, e.g.

GIT_INDEX_FILE=.git/tmpidx git reset
GIT_INDEX_FILE=.git/tmpidx git commit --amend
rm .git/tmpidx
like image 122
CB Bailey Avatar answered Nov 15 '22 09:11

CB Bailey


According to the man page git commit --amend --only without any paths specified should do the job, however this doesn't work for me. As a work-around you may temporarily add a file and remove it again, amending two times:

touch tmp
git add tmp
git commit --amend -m "new message" tmp
git rm tmp
git commit --amend -m "new message" tmp
like image 36
mstrap Avatar answered Nov 15 '22 07:11

mstrap