Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git, read latest commit message when committing

Tags:

git

git-commit

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?

like image 655
chwi Avatar asked Aug 17 '15 09:08

chwi


People also ask

How do I see the last commit message?

Viewing a list of the latest commits. If you want to see what's happened recently in your project, you can use git log . This command will output a list of the latest commits in chronological order, with the latest commit first.

How do I get the latest commit hash?

# open the git config editor $ git config --global --edit # in the alias section, add ... [alias] lastcommit = rev-parse HEAD ... From here on, use git lastcommit to show the last commit's hash. Save this answer.

How can I see my commit details?

`git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first.

What is the git command to see all the changes since the last commit?

By default git diff will show you any uncommitted changes since the last commit.


2 Answers

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.

like image 79
Noufal Ibrahim Avatar answered Oct 27 '22 19:10

Noufal Ibrahim


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.

like image 1
sdgluck Avatar answered Oct 27 '22 20:10

sdgluck