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)?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With