有没有一种方法可以重用git提交中的前一条注释



偶尔我会进入故障排除模式,提交/推送一些小但独立的提交,并附上注释,如"在部署到Heroku期间对<something>进行故障排除"。我希望每次提交都使用相同的注释,而无需重新键入。这可能吗?

从gitcommit(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.

一开始,我回答:

我想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

将提交更改(并最终提交新文件),并将"Troubleshooting thefooduring deployment to Heroku"作为提交消息。

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

git commit --file .git/COMMIT_EDITMSG

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

我不确定如何使用您输入的最后一个git注释来进行一组git提交,但您可以设置默认提交消息。只要在完成使用该消息所需的所有提交后取消设置默认提交消息,就可以实现这一点。

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

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

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

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

最新更新