【问题标题】:Git, read latest commit message when committingGit,提交时阅读最新的提交消息
【发布时间】:2015-11-09 20:54:36
【问题描述】:

当我提交时,这个文本会跳起来:

Please enter the commit message for your changes. Lines starting
 with '#' will be ignored, and an empty message aborts the commit.

 On branch master
 Your branch is ahead of 'origin/master' by 2 commits.

 Changes to be committed:

    new file:   modules/new_file.txt

我想要的是让这个信息丰富的文本也向我展示我上次提交的信息,而我不需要通过 git log、git show 或类似的东西。

例如

(...)

 Changes to be committed:

    new file:   modules/new_file.txt

 Previous commit message:
    [FIX] Fixed the foo.bar module

这与this question 完全相同,但没有一个答案实际上是在回答这个问题,所以我猜 OP 只是问的有点不对?

【问题讨论】:

  • 不,我正在使用带有 Atom、Sublime 或 Notepad++ 的 Windows。尚未决定,但目前我正在测试 Atom。

标签: git git-commit


【解决方案1】:

您可以编写自己的命令吗?它可能看起来像这样:

#!/bin/bash
echo "Last commit message:"
git log -1 --pretty=%B # only echo commit msg to console
echo "Enter commit message:"
read commitmsg # let user enter a commit message
git commit -m "$commitmsg"

然后您可以将此文件添加到您的 PATH。

【讨论】:

    【解决方案2】:

    有一个名为prepare-commit-msg 的git hook 生成此提交消息模板。默认情况下,您的.git 目录中应该有一个prepare-commit-msg.sample 文件。重命名它以删除 .sample,然后对其进行编辑以包含 git log -1 或您可能想要的任何其他内容,并且您会在提交时得到它。

    类似的东西

    #!/bin/sh
    
    echo "# Previous commit:" >> $1
    git log -1 -p|sed 's/^\(.\)/# \1/g'  >> $1
    

    应该够了。

    【讨论】:

    • 好的,我对 bash 脚本不是很熟悉,尝试在顶部添加 echo something、dir、ls 没有任何作用。你能提供一个最小的工作示例吗?编辑:我看到它在打开提交编辑器之前在 git bash 中吐出一些东西。所以我的问题是进入提交消息中的评论部分
    猜你喜欢
    • 2011-09-07
    • 2012-02-23
    • 2011-11-30
    • 2021-03-06
    • 2018-08-23
    • 2019-10-25
    • 2020-10-10
    • 2021-10-22
    • 2016-04-02
    相关资源
    最近更新 更多