Git,查看我最近N次提交的注释列表

217

有没有一种方法可以在Git中查看我最近N次提交的评论和时间列表?

在Stack Overflow上查找后,我只找到了一个相关的东西:Git - get all commits and blobs they created,但它显示所有用户的所有提交,并输出大量其他信息。

8个回答

324

如果你想使用命令行,可以使用 --author=<你的名字>

例如:要查看你最近的5个提交记录

git log -n 5 --author=Salvador

如果你想要更简单的一行代码解决方案:

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

编辑后添加

如果你喜欢单行版本,可以尝试创建一个别名来代替 git log,例如(这是我用于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"

现在,我只需要使用:

glog -n 5

然后我可以得到一个很好的输出结果,如下图所示:

终端输出

它被着色,显示了作者的名称并显示了图形,您仍然可以传入其他标志(例如--author),从而使过滤更加精确。


4
你可以节省两个按键,例如 git log -5。如果你正在一个脚本中限制输出的提交数量,应该友善地使用长选项,例如 git log --max-count=5 - Dennis
如果能在最后一行之后添加换行符就好了,但我找不到一个好的方法来实现这个。 - A.Robert
1
请注意:使用 git show -n 5 命令来查看最近 5 次提交的更改。 - Black
真的很好。特别是如果你与 --after="2022-12-1 等结合使用。 - Sentry.co

17

使用git log命令的--author和/或--committer过滤选项,以及-n选项来限制提交的数量。例如:

git log --author='Salvador Dali' -n 10

8
git log --format="%h %B" --oneline -n 1

这将获取最新的Git日志注释块和缩写的提交ID。
git log --format="%H %B" -n 1

这将为您提供包含完整提交ID的最新git日志注释块。
您可以从以下网址构建自己的格式:Git Pretty Format

使用 --oneline 选项将永远不会显示完整的提交 ID。此外,需要大写字母 H 才能显示完整的提交 ID。我建议尝试在没有 --oneline 的情况下使用这些命令进行实验。 - Joe Johnston
修复了潜水者的问题。 - Joe Johnston

5

git log --author="我的名字" -n 5 (查看所有替代方法,请参阅man git-log


3
如果你只需要最近的 X 条 Git 提交记录,下面的命令会返回最近的 5 条提交消息,每个消息以换行符分隔:
git log -5 --oneline --format=%s | sed 's/^.*: //'

会输出类似于这样的结果:
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
查看最近N次提交的评论列表。
git log --oneline -10

检出到较旧的提交

git ckeckout 3e6bb80

在切换到之前的提交后返回最新提交

git checkout -

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

请注意...yellow)%<(113,trunc),其中的113是要将注释修剪到的长度,允许完全自定义而不会被--oneline覆盖您的设置。

正如已经说过的,这可以被别名化,或者像我一样封装在powershell函数中。

以下内容超出了OP的范围,但为该线程带来了一些价值。

我知道我有些过分了,但这就是我们所做的。

    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 
}

1

less 一样工作,具有漂亮的格式,并且没有提示要继续:

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

运行得非常好


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接