210

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.

Community
  • 1
  • 1
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753

8 Answers8

317

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.

Dennis
  • 56,821
  • 26
  • 143
  • 139
Abizern
  • 146,289
  • 39
  • 203
  • 257
  • 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 03 '14 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 '15 at 07:23
  • 1
    Note: use `git show -n 5` to see the changes of the last 5 commits – Black Sep 11 '19 at 07:43
  • Really nice. Esp if you combine it with `--after="2022-12-1` etc – Sentry.co Jun 30 '23 at 13:46
16

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
Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
7
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

Joe Johnston
  • 2,794
  • 2
  • 31
  • 54
Firesh
  • 99
  • 1
  • 2
5

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

chelmertz
  • 20,399
  • 5
  • 40
  • 46
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 -
3

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
Dan Barclay
  • 5,827
  • 2
  • 19
  • 22
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 
}
Joe Johnston
  • 2,794
  • 2
  • 31
  • 54
1

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

aelkz
  • 1,786
  • 1
  • 24
  • 26