22

有时我会进入故障排除模式并提交/推送一些小的但单独的提交,并带有类似“在部署到 Heroku 期间对 <something> 进行故障排除”之类的评论。我想对每个提交使用相同的评论,而不必重新输入。这可能吗?

4

4 回答 4

35

git-commit(1)命令文档中,

-C <commit>
--reuse-message=<commit>
Take an existing commit object, and reuse the log message and the authorship 
information (including the timestamp) when creating the commit.

-c <commit>
--reedit-message=<commit>
Like -C, but with -c the editor is invoked, so that the user can further edit 
the commit message.

然后可以使用,

  git commit --reuse-message=HEAD

更新:

您可能还需要使用该--reset-author选项,

--reset-author
When used with -C/-c/--amend options, declare that the authorship of the 
resulting commit now belongs of the committer. This also renews the author 
timestamp.
于 2013-08-25T12:55:48.853 回答
18

一开始我是这样回答的:

我想git commit --reuse-message=HEAD是的

然后我认为那不是您想要的并删除了它。然后生活赶上了并AFK了几个小时。无论如何,尽管答案已经被接受,我还是建议:

$ git config alias.troubleshoot '!troubleshoot() { git add -u && git commit -m "Troubleshooting the $1 during deployment to Heroku."; }; troubleshoot'

您可以通过以下方式使用它:

  1. 修改现有文件
  2. (最终添加未跟踪的文件)
  3. git troubleshoot foo

将使用“在部署到 Heroku 期间对foo进行故障排除”来提交更改(并最终提交新文件) 。作为提交消息。

于 2013-08-25T12:46:56.277 回答
11

.git/COMMIT_EDITMSG 包含最后的提交消息。https://git-scm.com/docs/git-commit#_files

git commit --file .git/COMMIT_EDITMSG

将使用该文件作为提交消息。

于 2018-06-15T12:08:36.890 回答
1

我不确定如何让一组特定的 git 提交使用您输入的最后一个 git 评论,但您可以设置默认提交消息。只要您在完成所有需要使用该消息的提交后取消设置默认提交消息,就可以做到这一点。

以下是设置默认提交消息的方法。首先,在文件中输入所需的提交消息,我们调用它~/LastCommitMessage.txt。然后,将其指定为您的默认(全局)提交消息,如下所示:

$ git config --global commit.template ~/LastCommitMessage.txt

您可以通过不使用 --global 而使用其他东西来缩小范围。

您可以通过打开.gitconfig位于您的主目录中的文件轻松访问所有 git 设置。完成所有提交后,打开该文件并删除上面的设置以取消设置。

于 2013-08-25T12:55:47.533 回答