【问题标题】:Is there a way to reuse the previous comment on a git commit?有没有办法重用之前对 git commit 的评论?
【发布时间】:2013-08-28 01:19:06
【问题描述】:

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

【问题讨论】:

  • Visual Studio Code 允许您向上箭头到版本 1.51.0 之前的提交消息。

标签: git


【解决方案1】:

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

git commit --file .git/COMMIT_EDITMSG

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

【讨论】:

    【解决方案2】:

    一开始我的回答是:

    我猜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 进行故障排除”来提交更改(并最终提交新文件)。作为提交消息。

    【讨论】:

    • 这是一个更安全的问题解决方案,也是更广泛的问题的解决方案,如何轻松复用常见的 git commit cmets?您可以使用参数这一事实有助于确保日志也不会被无用的 cmets 填满。请参阅Aliases 了解更多信息。
    【解决方案3】:

    来自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.
    

    【讨论】:

    • 啊,所以这不是从我最近的提交评论中提取的?
    • 想一想...如果我在本地输入git commit -a --reuse-message=HEAD,除了我最近的提交评论之外,它怎么能提取任何内容?其他团队成员的评论如何通过该命令和上下文潜入其中?
    • 对不起,我误解了你的问题,是的,它是根据你的 HEAD 指向的位置从你最近的提交消息中提取的。如果您有另一条评论干扰了新评论和上一条评论,它将采用这条评论的消息。
    • 您可以使用给定的提交哈希值作为 --reuse-message 选项的值。 HEAD 的祖先也是可以访问的。
    【解决方案4】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      • 2015-10-28
      • 2015-12-11
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      相关资源
      最近更新 更多