Display last git commit comment

Git

Git Problem Overview


Often during a commit ($ git -commit -m ""), I wish to read my last comment to remember what progress I have made. Is there an easy way to directly access the last commit message through command-line? (I'm using Windows.)

Git Solutions


Solution 1 - Git

git show

is the fastest to type, but shows you the diff as well.

git log -1

is fast and simple.

git log -1 --pretty=%B

if you need just the commit message and nothing else.

Solution 2 - Git

Generally:

git log -n

will show you the last n commit messages

More elegantly - if you want a quick overview of your commits

git log --oneline -n

This will show just the first line of the last n commit messages.

You can save this as a git alias or a shell alias with a shorter command. I've got it in my shell as glog, for example, and I can see my last 10 commit messages with glog -10.

Solution 3 - Git

You can use

git show -s --format=%s

Here --format enables various printing options, see documentation here. Specifically, %smeans 'subject'. In addition, -s stands for --no-patch, which suppresses the diff content.

I often use

git show -s --format='%h %s'

where %h denotes a short hash of the commit

Another way is

git show-branch --no-name HEAD

It seems to run faster than the other way.

I actually wrote a small tool to see the status of all my repos, including the edit status, the relation to remote branch, etc. It also batch executes commands from any working directory.

You can find it on github.

enter image description here

Solution 4 - Git

git log -1 will display the latest commit message or git log -1 --oneline if you only want the sha1 and associated commit message to be displayed.

Solution 5 - Git

git log -1 branch_name will show you the last message from the specified branch (i.e. not necessarily the branch you're currently on).

Solution 6 - Git

For something a little more readable, run this command once:

git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"

so that when you then run:

git lg

you get a nice readout. To show only the last line:

git lg -1

Solution found here

Solution 7 - Git

To start with git log -1 --pretty='%s'

But the below one covers all the cases,

git log --pretty='format:%Creset%s' --no-merges -1

  • No unwanted white spaces
  • Discards merge commits
  • No commit id, date, Displays only the messages.

Paste & see for yourself

Solution 8 - Git

If you want to see just the subject (first line) of the commit message:

git log -1 --format=%s

This was not previously documented in any answer. Alternatively, the approach by nos also shows it.

Reference:

Solution 9 - Git

I did this

git reflog -1 | sed 's/^.*: //'

Solution 10 - Git

This command will get you the last commit message:

git log -1 --oneline --format=%s | sed 's/^.*: //'

outputs something similar to:

Create FUNDING.yml

You can change the -1 to any negative number to increase the range of commit messages retrieved

Solution 11 - Git

I just found out a workaround with shell by retrieving the previous command.

Press Ctrl-R to bring up reverse search command:

reverse-i-search

Then start typing git commit -m, this will add this as search command, and this brings the previous git commit with its message:

reverse-i-search`git commit -m`: git commit -m "message"

Enter. That's it!

(tested in Ubuntu shell)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwhamsicoreView Question on Stackoverflow
Solution 1 - GitCB BaileyView Answer on Stackoverflow
Solution 2 - GitAbizernView Answer on Stackoverflow
Solution 3 - GitnosView Answer on Stackoverflow
Solution 4 - GitGregory PakoszView Answer on Stackoverflow
Solution 5 - GitCJ DennisView Answer on Stackoverflow
Solution 6 - GitsungiantView Answer on Stackoverflow
Solution 7 - GitnehemView Answer on Stackoverflow
Solution 8 - GitAsclepiusView Answer on Stackoverflow
Solution 9 - GitLukePOLOView Answer on Stackoverflow
Solution 10 - GitDan BarclayView Answer on Stackoverflow
Solution 11 - GitSteve S.View Answer on Stackoverflow