Git,在提交时如何阅读最新的提交信息?

7
当我提交时,这段文本会跳起来:
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

这与此问题完全相同,但是没有一个答案实际回答了这个问题,所以我猜OP可能表述有误?


不,我正在使用Atom、Sublime或Notepad++等Windows平台上的编辑器。还没有决定用哪个,但目前我正在测试Atom。 - chwi
2个回答

5

有一个名为prepare-commit-msgGit钩子,用来生成提交信息模板。默认情况下,你的.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 somethingdirls并没有任何作用。你能提供一个最小化的工作示例吗?编辑:我看到它在打开提交编辑器之前在git bash中输出了一些内容。所以我的问题是如何进入提交消息的注释部分。 - chwi

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"

然后您将此文件添加到您的路径中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接