我想列出只是特定分支的一部分的所有提交。

使用下面的代码,它列出了来自分支的所有提交,也包括来自父(master)的所有提交

git log mybranch

我发现的另一种选择是排除主可达的提交,并给我我想要的,但我想避免知道其他分支名称的需要。

git log mybranch --not master

我试图使用git for-each-ref,但它也列出了mybranch,所以实际上它排除了所有:

git log mybranch --not $(git for-each-ref --format '^%(refname:short)' refs/heads/)

更新:

我正在测试一个我不久前发现的新选项,直到现在看来,这可能是我一直在寻找的:

git log --walk-reflogs mybranch

更新(2013 - 02 - 13 t15:08):

——walk-reflogs选项很好,但我检查了reflogs的过期时间(默认90天,gc.reflogExpire)。

我想我找到了我一直在寻找的答案

git log mybranch --not $(git for-each-ref --format='%(refname)' refs/heads/ | grep -v "refs/heads/mybranch")

我只是从可用的分支列表中删除当前分支,并使用该列表从日志中排除。这样我只能得到只有mybranch才能到达的提交。


当前回答

我终于找到了做上级想要的事的方法。其实很简单:

git log --graph [branchname] 

git log --graph origin/[branchname] # if your local checkout isn't up to date

该命令将以图形的格式显示从所提供的分支可到达的所有提交。但是,通过查看提交图(*是提交行中第一个字符),您可以轻松地过滤该分支上的所有提交。

例如,让我们看看下面cakephp GitHub repo上git log -graph master的摘录:

D:\Web Folder\cakephp>git log --graph master
*   commit 8314c2ff833280bbc7102cb6d4fcf62240cd3ac4
|\  Merge: c3f45e8 0459a35
| | Author: José Lorenzo Rodríguez <lorenzo@users.noreply.github.com>
| | Date:   Tue Aug 30 08:01:59 2016 +0200
| |
| |     Merge pull request #9367 from cakephp/fewer-allocations
| |
| |     Do fewer allocations for simple default values.
| |
| * commit 0459a35689fec80bd8dca41e31d244a126d9e15e
| | Author: Mark Story <mark@mark-story.com>
| | Date:   Mon Aug 29 22:21:16 2016 -0400
| |
| |     The action should only be defaulted when there are no patterns
| |
| |     Only default the action name when there is no default & no pattern
| |     defined.
| |
| * commit 80c123b9dbd1c1b3301ec1270adc6c07824aeb5c
| | Author: Mark Story <mark@mark-story.com>
| | Date:   Sun Aug 28 22:35:20 2016 -0400
| |
| |     Do fewer allocations for simple default values.
| |
| |     Don't allocate arrays when we are only assigning a single array key
| |     value.
| |
* |   commit c3f45e811e4b49fe27624b57c3eb8f4721a4323b
|\ \  Merge: 10e5734 43178fd
| |/  Author: Mark Story <mark@mark-story.com>
|/|   Date:   Mon Aug 29 22:15:30 2016 -0400
| |
| |       Merge pull request #9322 from cakephp/add-email-assertions
| |
| |       Add email assertions trait
| |
| * commit 43178fd55d7ef9a42706279fa275bb783063cf34
| | Author: Jad Bitar <jadbitar@mac.com>
| | Date:   Mon Aug 29 17:43:29 2016 -0400
| |
| |     Fix `@since` in new files docblocks
| |

正如您所看到的,只有8314c2ff833280bbc7102cb6d4fcf62240cd3ac4和c3f45e811e4b49fe27624b57c3eb8f4721a4323b在提交行中将*作为第一个字符。这些提交来自主分支,而其他四个提交来自其他分支。

其他回答

快速回答:

git log $(git merge-base master b2)..HEAD

比方说:

That you have a master branch Do a few commits You created a branch named b2 Do git log -n1; the commit Id is the merge base between b2 and master Do a few commits in b2 git log will show your log history of b2 and master Use commit range, if you aren't familiar with the concept, I invite you to google it or stack overflow-it, For your actual context, you can do for example git log commitID_FOO..comitID_BAR The ".." is the range operator for the log command. That mean, in a simple form, give me all logs more recent than commitID_FOO... Look at point #4, the merge base So: git log COMMITID_mergeBASE..HEAD will show you the difference Git can retrieve the merge base for you like this git merge-base b2 master Finally you can do: git log $(git merge-base master b2)..HEAD

你可以尝试这样做:

#!/bin/bash

all_but()
{
    target="$(git rev-parse $1)"
    echo "$target --not"
    git for-each-ref --shell --format="ref=%(refname)" refs/heads | \
    while read entry
    do
        eval "$entry"

        test "$ref" != "$target" && echo "$ref"
    done
}

git log $(all_but $1)

或者,借用Git用户手册中的食谱:

#!/bin/bash
git log $1 --not $( git show-ref --heads | cut -d' ' -f2 | grep -v "^$1" )

我发现这种方法相对简单。

结帐到分行和比

运行 -2 HEAD . git

这将只提供两个sha:

1)分支的最后一次提交[C1]

2)和提交父分支的第一次提交[C2]

现在运行 git log——decoration——pretty=oneline——reverse——name-status <C2>..<C1> .

这里C1和C2是运行第一个命令时将得到的两个字符串。在第二个命令中输入这些不包含<>的值。

这将给出分支内文件更改历史的列表。

我终于找到了做上级想要的事的方法。其实很简单:

git log --graph [branchname] 

git log --graph origin/[branchname] # if your local checkout isn't up to date

该命令将以图形的格式显示从所提供的分支可到达的所有提交。但是,通过查看提交图(*是提交行中第一个字符),您可以轻松地过滤该分支上的所有提交。

例如,让我们看看下面cakephp GitHub repo上git log -graph master的摘录:

D:\Web Folder\cakephp>git log --graph master
*   commit 8314c2ff833280bbc7102cb6d4fcf62240cd3ac4
|\  Merge: c3f45e8 0459a35
| | Author: José Lorenzo Rodríguez <lorenzo@users.noreply.github.com>
| | Date:   Tue Aug 30 08:01:59 2016 +0200
| |
| |     Merge pull request #9367 from cakephp/fewer-allocations
| |
| |     Do fewer allocations for simple default values.
| |
| * commit 0459a35689fec80bd8dca41e31d244a126d9e15e
| | Author: Mark Story <mark@mark-story.com>
| | Date:   Mon Aug 29 22:21:16 2016 -0400
| |
| |     The action should only be defaulted when there are no patterns
| |
| |     Only default the action name when there is no default & no pattern
| |     defined.
| |
| * commit 80c123b9dbd1c1b3301ec1270adc6c07824aeb5c
| | Author: Mark Story <mark@mark-story.com>
| | Date:   Sun Aug 28 22:35:20 2016 -0400
| |
| |     Do fewer allocations for simple default values.
| |
| |     Don't allocate arrays when we are only assigning a single array key
| |     value.
| |
* |   commit c3f45e811e4b49fe27624b57c3eb8f4721a4323b
|\ \  Merge: 10e5734 43178fd
| |/  Author: Mark Story <mark@mark-story.com>
|/|   Date:   Mon Aug 29 22:15:30 2016 -0400
| |
| |       Merge pull request #9322 from cakephp/add-email-assertions
| |
| |       Add email assertions trait
| |
| * commit 43178fd55d7ef9a42706279fa275bb783063cf34
| | Author: Jad Bitar <jadbitar@mac.com>
| | Date:   Mon Aug 29 17:43:29 2016 -0400
| |
| |     Fix `@since` in new files docblocks
| |

正如您所看到的,只有8314c2ff833280bbc7102cb6d4fcf62240cd3ac4和c3f45e811e4b49fe27624b57c3eb8f4721a4323b在提交行中将*作为第一个字符。这些提交来自主分支,而其他四个提交来自其他分支。

git rev-list --exclude=master --branches --no-walk

会列出每个非主分支的提示。

git rev-list master --not $(git rev-list --exclude=master --branches --no-walk)

将列出master历史记录中没有在任何其他分支历史记录中的每个提交。

排序对于为提交选择设置过滤器管道的选项很重要,因此——branches必须遵循它应该应用的任何排除模式,而——no-walk必须遵循提供提交的过滤器,rev-list不应该行走。