如果由于某种原因提交失败,git 提交消息是否可以恢复?

OJF*_*ord 7 git

git commit可能会因gpg.commitsign = true&&gpg失败(无论出于何种原因)等原因而失败。重试该命令会打开一个空白编辑器;消息丢失。

发生这种情况时,是否有任何方法可以恢复写入的提交消息,以使用相同的消息重试提交?

OJF*_*ord 9

man git-commit

FILES
       $GIT_DIR/COMMIT_EDITMSG
           This file contains the commit message of a commit in progress. If git commit exits due to an error before creating a commit, any commit message that has been provided
           by the user (e.g., in an editor session) will be available in this file, but will be overwritten by the next invocation of git commit.
Run Code Online (Sandbox Code Playgroud)

git commit因此,可以使用以下命令重试上一条消息,而不是重复:

$ git commit -m "$(cat .git/COMMIT_EDITMSG)"
Run Code Online (Sandbox Code Playgroud)

或者一般情况(例如适合别名):

$ git commit -m "$(cat "$(git rev-parse --git-dir)/COMMIT_EDITMSG)")"
Run Code Online (Sandbox Code Playgroud)