像树一样输出git branch在树中的输出


160

现在,当我输入“ git branch”时

它以任意顺序列出我的分支。

我更希望的是,如果“ git branch”将我的输出列出在像fasion这样的树中,例如:

master
|-- foo
  |-- foo1
  |-- foo2
|-- bar
  |-- bar4

在这里,foo&bar是从master分支出来的;foo1和foo2从foo分支;bar4是从bar分支出来的。

这容易实现吗?

[仅命令行实用程序。这需要适合我的zsh / vim工作流程。]


这里的所有答案(包括我自己的答案)似乎都无法为我认为您真正想要的以及我所知道的提供适当的解决方案。如果有机会,我将编写一个新的实用程序来解决此问题。可能会称之为git_tree。它将输出类似arc flow此处的内容:stackoverflow.com/questions/54227968/…。也许有一天我什至可以将其合并到git本身。
加布里埃尔·斯台普斯


git log --graph我认为就足够了。
DawnSong

Answers:


203

下面答案使用git log

我在2009年用“ 无法在终端中显示Git树 ”提到了类似的方法:

git log --graph --pretty=oneline --abbrev-commit

但是我一直在使用的完整内容是在“ 如何使用git log --graph显示标签名称和分支名称 ”(2011)中:

git config --global alias.lgb "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset%n' --abbrev-commit --date=relative --branches"

git lgb

原始答案(2010年)

git show-branch --list 接近您要寻找的东西(与topo命令一起使用)

--topo-order

默认情况下,分支及其提交按相反的时间顺序显示。
此选项使它们按拓扑顺序显示(即,后代提交在其父之前显示)。

但是工具git wtf也可以提供帮助。例:

$ git wtf
Local branch: master
[ ] NOT in sync with remote (needs push)
    - Add before-search hook, for shortcuts for custom search queries. [4430d1b] (edwardzyang@...; 7 days ago)
Remote branch: origin/master (git@gitorious.org:sup/mainline.git)
[x] in sync with local

Feature branches:
{ } origin/release-0.8.1 is NOT merged in (1 commit ahead)
    - bump to 0.8.1 [dab43fb] (wmorgan-sup@...; 2 days ago)
[ ] labels-before-subj is NOT merged in (1 commit ahead)
    - put labels before subject in thread index view [790b64d] (marka@...; 4 weeks ago)
{x} origin/enclosed-message-display-tweaks merged in
(x) experiment merged in (only locally)

NOTE: working directory contains modified files

git-wtf 显示给您:

  • 如果是跟踪分支,则分支与远程回购的关系。
  • 如果您的分支是功能分支,则它与非功能(“版本”)分支的关系。
  • 如果是版本分支,则分支与功能分支的关系

正在使用您的漂亮格式的一种变体,其中还使用%ae显示了作者的电子邮件。我也更喜欢用“ sl”来称呼别名,类似于hg的smartlog。
fiorix

非常感谢该git-wtf工具的链接,它非常有用。似乎从本质上分解了我盯着一棵花哨的git log树得出的结论,但总结不错。
卢克·戴维斯

我希望找到一种方法,以便结帐分支应显示从哪个分支中结帐
Chang Zhao

@ChangZhao这类似于“寻找母店”,这是不容易做到的:stackoverflow.com/a/3162929/6309stackoverflow.com/a/56452713/6309
VonC

147

这不是您想要的,但是

git log --graph --simplify-by-decoration --pretty=format:'%d' --all

做得很好。它还显示标签和远程分支。这可能不是每个人都希望的,但我发现它很有用。--simplifiy-by-decoration这是限制显示的裁判的绝招。

我使用类似的命令来查看我的日志。我已经可以gitk用它完全取代我的用法:

git log --graph --oneline --decorate --all

我通过在〜/ .gitconfig文件中包含以下别名来使用它:

[alias]
    l = log --graph --oneline --decorate
    ll = log --graph --oneline --decorate --branches --tags
    lll = log --graph --oneline --decorate --all

编辑:更新了建议的日志命令/别名以使用更简单的选项标志。


1
IMO这是最好的答案,但是我认为SourceTree或gitk之类是解决这类问题的方法。
JaKXz 2014年

这将在原点显示分支。有什么办法让它显示在本地分支机构吗?
杰夫2015年

@Jeff更换--all--branches --tags可能会做到这一点。
nocash15年

完美的答案。我在寻找什么,我在这里找到了。大。
AMIC MING

12

以下示例还显示了提交父母:

git log --graph --all \
--format='%C(cyan dim) %p %Cred %h %C(white dim) %s %Cgreen(%cr)%C(cyan dim) <%an>%C(bold yellow)%d%Creset'


6

在Ubuntu上测试:

sudo apt install git-extras
git-show-tree

这产生的效果类似于此处最受好评的两个答案。

资料来源:http : //manpages.ubuntu.com/manpages/bionic/man1/git-show-tree.1.html


另外,如果您已经安装了arcanist(更正:安装了Uber的arcanist叉子 - 有关安装说明,请参见此答案的底部),arc flow显示了上游依赖关系的漂亮依赖关系树(即:以前是通过arc flow new_branch或通过手动设置的)git branch --set-upstream-to=upstream_branch)。

额外的git技巧:

有关:

  1. “弧形嫁接”和“弧形补丁”之间有什么区别?

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.