如何使用 git log --graph 只显示分支头?

3

如何使用 git log --graph 只包含当前分支的头部提交?

例如:

$ git log --graph --all --pretty=format:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative

提供:

*   fa3d2f92d - (HEAD -> feat/pullupstream) Merge remote-tracking branch 'foundation/master' into feat/pullupstream (48 minutes ago) <me>
|\  
* | a12984977 - (myuser/feat/chainconfig-equivalence-test, mycompany/feat/chainconfig-equivalence-test, feat/istanbul-featureblocks, feat/chainconfig-equivalence-test) Makefile: add test-multigeth command (3 hours ago) <Jeff>
* | 7c471933a - tests: add equivalence-proof tests for fork feature configs (3 hours ago) <Jeff>
* | 47189785b - params: remove and comment CheckConfigForkOrder (3 hours ago) <Jeff>
* | 87d575b8f - params: remove unused IsEIP158 method (3 hours ago) <Jeff>
* | aaf58e484 - core/vm: fix commented fork names (3 hours ago) <Jeff>
* | 1771de385 - core: prefer EIP161F feat instead of EIP158 (3 hours ago) <Jeff>
* | 30ded24cc - tests: s/IsEIP158/IsEIP161F/ (4 hours ago) <Jeff>
* | 7839340cc - Makefile: add env var to enable args to test cmd (4 hours ago) <Jeff>
* | c398d1231 - (mycompany/feat/verbose-test-logs, feat/verbose-test-logs) build: add test cmd flag -v for verbose logs (4 hours ago) <Jeff>
| | * 488a69b8b - (feat/x-chainspec) consensus/ethash,params: refactor HasECIP1017->IsECIP1017F (78 minutes ago) <Jeff>
| | * 456aa8cc9 - params: add ECIP1017FBlock to satisfy forkid implementation (78 minutes ago) <Jeff>
| | * b945c3902 - core/forkid: test gatherForks func against ETC conf (78 minutes ago) <Jeff>
| | * 3e0527dcb - cmd/utils: refactor datadir from --chain context (78 minutes ago) <Jeff>
| | * 6b1d6b6d5 - cmd/utils: refactor --[chain] -> genesis logic (78 minutes ago) <Jeff>
| | * 54e5272c2 - chainspec,cmd/faucet|geth|utils,eth,mobile,params: refactor NetworkId (78 minutes ago) <Jeff>
| | * aa18677ce - chainspec: init support for Parity/Clique config (78 minutes ago) <Jeff>
| | * e46bcfb8e - cmd/puppeth,+chainspec: move chainspec data structures to own package (78 minutes ago) <Jeff>
| | * 6ec3edff4 - cmd/puppeth: export ChainSpecs and New_ methods (78 minutes ago) <Jeff>
| | *   864e57887 - (multi-geth/master, master) Merge pull request #124 from mycompany/fix/mordor-genesis-timestamp (9 hours ago) <Tang>
| | |\  
| | | * dc17c5d96 - (mycompany/fix/mordor-genesis-timestamp, fix/mordor-genesis-timestamp) core: set mordor genesis block timestamp (2 days ago) <Jeff>
| | |/  
| | | * fde76dc96 - (mycompany.technology.go-technology/feat/makefile-test-cmd-args-ef, feat/makefile-test-cmd-args-ef) Makefile: add env var to enable args to test cmd (2 hours ago) <Jeff>
| | |/  
| |/|   
| | | * b77fa876f - (mycompany.technology.go-technology/feat/verbose-test-logs-ef, feat/verbose-test-logs-ef) build: add test cmd flag -v for verbose logs (2 hours ago) <Jeff>
| | |/  
| |/|   
| * | 738b51ae3 - (foundation/master, foundation/HEAD, mycompany.technology.go-technology/foundation-master, foundation-master) core/vm: fix tracer interface parameter name (#20294) (11 hours ago) <nejsad4>
| * | f03b2db7d - params: finish sentence in comment (#20291) (23 hours ago) <myuser>
| * | 49d1a032d - build: gather info to investigate why builds fail on ARM (#20281) (30 hours ago) <Guillaume>
| * | 765fe446c - whisper/whisperv6: fix staticcheck issues (#20288) (35 hours ago) <Guillaume>

但我只想获取与分支引用相关的提交记录:

*   fa3d2f92d - (HEAD -> feat/pullupstream, gogs/feat/pullupstream) Merge remote-tracking branch 'foundation/master' into feat/pullupstream (49 minutes ago) <me>
|\  
* | a12984977 - (myuser/feat/chainconfig-equivalence-test, mycompany/feat/chainconfig-equivalence-test, feat/istanbul-featureblocks, feat/chainconfig-equivalence-test) Makefile: add test-multigeth command (3 hours ago) <Jeff>
* | c398d1231 - (mycompany/feat/verbose-test-logs, feat/verbose-test-logs) build: add test cmd flag -v for verbose logs (4 hours ago) <Jeff>
| | * 488a69b8b - (feat/x-chainspec) consensus/ethash,params: refactor HasECIP1017->IsECIP1017F (80 minutes ago) <Jeff>
| | *   864e57887 - (multi-geth/master, master) Merge pull request #124 from mycompany/fix/mordor-genesis-timestamp (9 hours ago) <Tang>
| | |\  
| | | * dc17c5d96 - (mycompany/fix/mordor-genesis-timestamp, fix/mordor-genesis-timestamp) core: set mordor genesis block timestamp (2 days ago) <Jeff>
| | |/  
| | | * fde76dc96 - (mycompany.technology.go-technology/feat/makefile-test-cmd-args-ef, feat/makefile-test-cmd-args-ef) Makefile: add env var to enable args to test cmd (2 hours ago) <Jeff>
| | |/  
| |/|   
| | | * b77fa876f - (mycompany.technology.go-technology/feat/verbose-test-logs-ef, feat/verbose-test-logs-ef) build: add test cmd flag -v for verbose logs (2 hours ago) <Jeff>
| | |/  
| |/|   
| * | 738b51ae3 - (foundation/master, foundation/HEAD, mycompany.technology.go-technology/foundation-master, foundation-master) core/vm: fix tracer interface parameter name (#20294) (11 hours ago) <nebojsa94>
2个回答

2
您甚至可以更好地控制Git 2.38(2022年第三季度)中的--decorate-refs:默认情况下,“log --decorate”使用的名称空间从“refs/”层次结构已被收紧。
参见 提交 992f25d提交 863a8ae提交 3e103ed提交 748706d提交 9215629提交 94d421b提交 97e61e0提交 b9342b3提交 5797b13提交 b004521提交 b877e61(2022年8月5日)作者为Derrick Stolee(derrickstolee
(由Junio C Hamano -- gitster --合并于提交 c068a3b,2022年8月29日) 日志:添加默认装饰过滤器 签名:Derrick Stolee

When a user runs 'git log'(man), they expect a certain set of helpful decorations.
This includes:

  • The HEAD ref
  • Branches (refs/heads/)
  • Stashes (refs/stash)
  • Tags (refs/tags/)
  • Remote branches (refs/remotes/)
  • Replace refs (refs/replace/ or $GIT_REPLACE_REF_BASE)

Each of these namespaces was selected due to existing test cases that verify these namespaces appear in the decorations.
In particular, stashes and replace refs can have custom colors from the color.decorate.<slot> config option.

While one test checks for a decoration from notes, it only applies to the tip of refs/notes/commit (or its configured ref name).
Notes form their own kind of decoration instead.

There are several tests throughout the codebase that verify that --decorate-refs, --decorate-refs-exclude, and log.excludeDecoration work as designed.

However, there are other refs that are less helpful to show as decoration:

  • Prefetch refs (refs/prefetch/)
  • Rebase refs (refs/rebase-merge/ and refs/rebase-apply/)
  • Bundle refs (refs/bundle/) [!]

[!] The bundle refs are part of a parallel series that bootstraps a repo from a bundle file, storing the bundle's refs into the repo's refs/bundle/ namespace.

In the case of prefetch refs, 96eaffe ("maintenance: set log.excludeDecoration during prefetch", 2021-01-19, Git v2.31.0-rc0 -- merge listed in batch #5) added logic to add refs/prefetch/ to the log.excludeDecoration config option.

Additional feedback pointed out that having such a side-effect can be confusing and perhaps not helpful to users.
Instead, we should hide these ref namespaces that are being used by Git for internal reasons but are not helpful for the users to see.

The way to provide a seamless user experience without setting the config is to modify the default decoration filters to match our expectation of what refs the user actually wants to see.

In builtin/log.c, after parsing the --decorate-refs and --decorate-refs-exclude options from the command-line, call set_default_decoration_filter().
This method populates the exclusions from log.excludeDecoration, then checks if the list of pattern modifications are empty.
If none are specified, then the default set is restricted to the set of inclusions mentioned earlier (HEAD, branches, etc.).

The 'decoration' value is non-zero when that namespace is associated with a special coloring and fits into the list of "expected" decorations as described above, which makes the implementation of this filter very simple.

Note that the logic in ref_filter_match() in log-tree.c follows this matching pattern:

  1. If there are exclusion patterns and the ref matches one, then ignore the decoration.
  2. If there are inclusion patterns and the ref matches one, then definitely include the decoration.
  3. If there are config-based exclusions from log.excludeDecoration and the ref matches one, then ignore the decoration.

With this logic in mind, we need to ensure that we do not populate our new defaults if any of these filters are manually set.
Specifically, if a user runs

git -c log.excludeDecoration=HEAD log

then we expect the HEAD decoration to not appear.
If we left the default inclusions in the set, then HEAD would match that inclusion before reaching the config-based exclusions.

A potential alternative would be to check the list of default inclusions at the end, after the config-based exclusions.
This would still create a behavior change for some uses of --decorate-refs-exclude=<X>, and could be overwritten somewhat with --decorate-refs=refs/ and --decorate-refs=HEAD.
However, it no longer becomes possible to include refs outside of the defaults while also excluding some using log.excludeDecoration.

Another alternative would be to exclude the known namespaces that are not intended to be shown.
This would reduce the visible effect of the change for expert users who use their own custom ref namespaces.

It is critical that we provide ways for expert users to disable this behavior change via command-line options and config keys.

"git log"现在在其手册页面中包括以下内容:

如果没有给出这些选项或配置设置,则引用作为装饰,如果它们与HEADrefs/heads/refs/remotes/refs/stash/refs/tags/匹配。

并且:

log:添加--clear-decorations选项

签名作者:Derrick Stolee

先前的更改在“git log”(man)命令中引入了一个新的默认ref过滤器,可以使用--decorate-refs=HEAD--decorate-refs=refs/进行覆盖,但这对用户来说很麻烦。相反,添加一个--clear-decorations选项,将所有先前的过滤器重置为接受所有ref的空白过滤器。现在,“git log”在其手册页面中包含了这些更改。 英译中:

--clear-decorations

当指定该选项时,将清除所有先前的 --decorate-refs--decorate-refs-exclude 选项并放宽默认的装饰过滤器以包括所有引用。

示例:

git log --decorate=full --pretty="tformat:%f%d" \
        --simplify-by-decoration \
        --decorate-refs-exclude="heads/octopus*" \
        --decorate-refs="heads" \
        --clear-decorations

2
您正在寻找--simplify-by-decoration选项。
您还可以使用--decorate-refs / --decorate-refs-exclude来过滤要显示的参考文献:
# will only keep branches (not tags) starting with "feature/" or "bug/" :
git log --oneline --graph --simplify-by-decoration
    --decorate-refs=refs/heads/feature
    --decorate-refs=refs/heads/bug

# will keep all refs and exclude tags starting with "private/" :
git log --oneline --graph --simplify-by-decoration
    --decorate-refs-exclude=refs/tags/private

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