Git, read latest commit message when committing

老子叫甜甜 提交于 2019-12-10 13:24:35

问题


When I am committing, this text jumps up:

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

What I want is to let this informative text also show me the message of my last commit, without me needing to go through git log, git show or anything similar.

E.g.

(...)

 Changes to be committed:

    new file:   modules/new_file.txt

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

This is exactly the same as this question, but none of the answers was actually answering the question, so I guess OP just asked it a bit wrong?


回答1:


There is a git hook called prepare-commit-msg which is what generates this commit message template. There should be a prepare-commit-msg.sample file in your .git directory by default. Rename it to remove the .sample and then edit it to include a git log -1 or anything else you might want and you'll get it when you commit.

Something like this

#!/bin/sh

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

should be enough.




回答2:


You could write your own command? It might look something like this:

#!/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"

You would then add this file to your PATH.



来源:https://stackoverflow.com/questions/32046680/git-read-latest-commit-message-when-committing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!