我知道我可以使用git commit --amend --file=path-to-my-new-message
,但这也会修改分阶段的更改。当然,我可以存储并稍后应用和删除存储,但有没有更快的解决方案来更改HEAD提交消息而不提交暂存的更改(和用户交互)?
答案 0 :(得分:7)
您可以将新的提交消息写入文件(例如msg.txt
)并使用git commit-tree
,例如
new_head=$(git commit-tree HEAD^{tree} -p HEAD^ <msg.txt)
git reset --soft $new_head
这假定您要修改的提交有一个父级,如果不是,则需要提供更多-p HEAD^2 -p HEAD^3 ...
。
这有点丑陋和低级别。您可能会发现更容易隐藏更改并使用直接“修改”。
git stash
git commit --amend
git stash pop --index
正如@Jefromi建议的那样,您也可以使用临时索引文件进行修改操作,例如
GIT_INDEX_FILE=.git/tmpidx git reset
GIT_INDEX_FILE=.git/tmpidx git commit --amend
rm .git/tmpidx
答案 1 :(得分:2)
根据手册页git commit --amend --only
没有指定任何路径应该完成这项工作,但是这对我不起作用。作为解决方法,您可以暂时添加一个文件并将其删除,修改两次:
touch tmp
git add tmp
git commit --amend -m "new message" tmp
git rm tmp
git commit --amend -m "new message" tmp
答案 2 :(得分:0)
您可以在{git-rebase打开的编辑器中git rebase -i HEAD^
然后将pick
更改为reword
。在此之后,系统将提示您输入新的提交消息。