223

Is there a way to see a list of comments and time of my last N commits in Git?

After looking on SO, the only relevant thing I have found is Git - get all commits and blobs they created, but it shows all commits from all users, and outputs a lot of other information.

0

8 Answers 8

330

If you want to use the command line you can use the --author=<your name>

For example: to see your last 5 commits

git log -n 5 --author=Salvador

If you want a simpler one line solution:

git log --oneline -n 5 --author=Salvador

Edited to add

If you like the single line version, try creating an alias for git log like this (this is what I have for zsh)

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

Now, I can just use:

glog -n 5

And I get a nice output such as:

Terminal output

Which is colourised, shows the name of the author and also shows the graph and you can still pass in other flags (such as --author) which lets you filter it even more.

4
  • 4
    You can save two keystrokes, e.g. git log -5. If you're limiting the number of commits to output within a script, you should be kind to others and use the long option, e.g. git log --max-count=5.
    – Dennis
    Apr 3, 2014 at 18:21
  • It would be nice if a newline could be appended after the last line, but I couldn't find a good way to do that.
    – A.Robert
    Jul 15, 2015 at 7:23
  • 1
    Note: use git show -n 5 to see the changes of the last 5 commits
    – Black
    Sep 11, 2019 at 7:43
  • Really nice. Esp if you combine it with --after="2022-12-1 etc
    – Sentry.co
    Jun 30, 2023 at 13:46
17

Use the --author and/or --committer filtering options with git log, plus the -n option to limit the number of commits. For example:

git log --author='Salvador Dali' -n 10
8
git log --format="%h %B" --oneline -n 1

This will get you latest git log comment block with abbreviated commit id.

git log --format="%H %B" -n 1

This will get you latest git log comment block with full commit id.

You can build your own format from : Git Pretty Format

2
  • using the --oneline will never show full commit id. Also, capital H is required to show full commit id. I suggest experimenting with the commands without the --oneline Oct 8, 2021 at 18:46
  • Fixed it for the lurkers Oct 8, 2021 at 18:47
5

git log --author="My name" -n 5 (see man git-log for all alternatives)

4

If you are after only the last X git commit messages, the following command will give you the last 5 commit messages as strings separated by new lines:

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

will output something like this:

Remove references to Node 8
Move ESLint cache file into node_modules
Update postcss packages
Add TypeScript 4.x as peerDependency to react-scripts
Create FUNDING.yml
3

See a list of comments of last N commits

git log --oneline -10

Check out to an older commit

git ckeckout 3e6bb80

Get back to the latest commit after checking out a previous commit

git checkout -
2

working like less with pretty format and without prompt to continue:

git --no-pager log --decorate=short --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' -n5

works beautifully

1

git log --max-count=15 --pretty="format:%C(dim green) %<(9,trunc)%ar %C(bold magenta)%h %C(bold green)%<(12,trunc)%an %C(bold yellow)%<(113,trunc)%s" --no-merges

Notice ...yellow)%<(113,trunc) the 113 is the length to trim the comments to allowing full customization without --oneline overriding your settings.

As has been said this could be aliased or as I have wrapped in a powershell function.

The following is beyond the OP but brings some value to the thread.

I know I got carried away but it is what we do.

    function logs() {
<#
    .SYNOPSIS 
     Shows my logs  
    .DESCRIPTION 
     Returns an abreviated list of logs meeting the filtering provided including max returned, committor by case sensitive pattern, branch, local or remote, and a 'my' shourcut to get the callers commits only
    .EXAMPLE
    PS>logs
     
    [ Basic usage gets maximum 15 logs from the origin/<current branch> ]


 origin/master logs

git log origin/master --max-count=15 --pretty="format:%C(dim green) %<(9,trunc)%ar %C(bold magenta)%h %C(bold green)%<(12,trunc)%an %C(bold yellow)%<(113,trunc)%s"

 2 days .. b6e4d0b Joe Johnston Added Posh
 2 days .. 0f1a166 Joe Johnston Updated the profile system
 4 days .. dfd3115 Joe Johnston added .net install and pinned applications. Updated git functions
 6 weeks.. 47bd9e9 Joe Johnston updated functions
 3 month.. 5148f09 Joe Johnston initial add

    .EXAMPLE
    PS>logs -l
     
    [ Usage gets maximum 15 local logs from the <current branch> ]


  logs

git log  --max-count=15 --pretty="format:%C(dim green) %<(9,trunc)%ar %C(bold magenta)%h %C(bold green)%<(12,trunc)%an %C(bold yellow)%<(113,trunc)%s"

 3 hours.. efb36e9 Joe Johnston updated profile to set-execution
 3 hours.. 4355a00 Joe Johnston Merge branch 'master' of https://github.com/xxx
 3 hours.. 84cd380 Joe Johnston updated gitfunctions - added undomerge
 2 days .. b6e4d0b Joe Johnston Added Posh
 2 days .. 0f1a166 Joe Johnston Updated the profile system
 4 days .. dfd3115 Joe Johnston added .net install and pinned applications. Updated git functions
 6 weeks.. 47bd9e9 Joe Johnston updated functions
 3 month.. 5148f09 Joe Johnston initial add

     
    .EXAMPLE
     logs 25

     [ Usage gets maximum 25 logs from the origin/<current branch> ]
    .EXAMPLE
     logs -m -c 20

     [ Usage gets maximum 20 local logs from the <current branch> commited by me]
    .EXAMPLE
     logs -b dev/iOS -c 25 -l -c "Jackson"

     [ Usage gets maximum 20 local logs from the <current branch> commited by the <pattern> Jackson]
#>
    [cmdletbinding()]
    Param(
    [parameter(Mandatory=$false,ValueFromPipeline)]
    [Alias('c')]
        [int]$Count = 15,
    [parameter(Mandatory=$false,ValueFromPipeline)]
    [Alias('b')]
        [string]$Branch = "Current",
    [parameter(Mandatory=$false,ValueFromPipeline)]
    [Alias('u')]
    [Alias('user')]
        [string]$Committer = "",
    [parameter(Mandatory=$false,ValueFromPipeline)]
    [Alias('m')]
    [Alias('me')]
    [Alias('onlyme')]
        [switch]$My = $false,
    [parameter(Mandatory=$false,ValueFromPipeline)]
    [Alias('g')]
        [switch]$Graph = $false,
    [parameter(Mandatory=$false,ValueFromPipeline)]

    [Alias('sm')]
    [Alias('merge')]
        [switch]$ShowMerges = $false,
    [parameter(Mandatory=$false,ValueFromPipeline)]

    [Alias('r')]
        [switch]$Remote = $false
    )
    $merge = '--no-merges';
    if ($ShowMerges) {
        $merge = '';
    }
    $Pretty = "--pretty=`"format:%C(dim green) %<(9,trunc)%ar %C(bold magenta)%h %C(bold green)%<(12,trunc)%an %C(bold yellow)%<(113,trunc)%s`"";
    #git config --global format.pretty $Pretty 
    if ($Branch -eq "Current") { 
        $Branch = git symbolic-ref --short HEAD; 
        write-host "************************************************";
        } else { 
        write-host "================================================";
        }

    if ($Remote -eq $true) { $Where = "origin/$Branch"; }
    if ($Graph -eq $true) { $GraphTag = "--graph"; }
    
     if([string]::IsNullOrEmpty($Committer) -eq $false) {
        $Who = $Committer;
        $Committer = "--committer=" + $Committer;
        write-host $Who
     }

    if ($My -eq $true) { 
        $me = git config user.name;
        $Committer = "--committer=`"$me`"";
        $Who = "**MY**";
    }

    write-host "$Who $Where logs" -foregroundcolor "Red";
    $commandOut = "git log $Where $GraphTag --max-count=$Count $Pretty $Committer $GraphTag $merge";
    write-Verbose $commandOut;
    write-host;

    git log $Where --max-count=$Count $Pretty $Committer $GraphTag $merge  
    write-host 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.