代码之家  ›  专栏  ›  技术社区  ›  anon

以树状方式输出git分支

git
  •  107
  • anon  · 技术社区  · 15 年前

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

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

    我更希望“git branch”将我的输出列为树状的形式,比如:

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

    在这里,foo&bar从master;foo1&foo2从foo;bar4从bar分支。

    这容易做到吗?

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

    5 回复  |  直到 6 年前
        1
  •  133
  •   Community Mohan Dere    8 年前

    这个 answer below 使用 git log :

    我在2009年提到过类似的方法 Unable to show a Git tree in terminal “:

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

    但我一直在用的是 How to display the tag name and branch name using 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-order
    

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

    但是工具 git wtf 可以 help too . 例子:

    $ 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 告诉你:

    • 如果是跟踪分支,则分支与远程回购的关系。
    • 如果分支是功能分支,则分支与非功能(“版本”)分支的关系。
    • 如果是版本分支,则分支与功能分支的关系
        2
  •  124
  •   nocash    11 年前

    不是你要的,但是

    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
    

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

        3
  •  10
  •   Vladimir Prudnikov    14 年前

    你可以使用一个叫做 gitk .

        4
  •  9
  •   Benjamin W.    9 年前

    下面的示例还显示了提交父级:

    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'
    
        5
  •  0
  •   Gabriel Staples    6 年前

    在ubuntu上测试:

    sudo apt install git-extras
    git-show-tree
    

    这产生的效果类似于这里两个最高票的答案。

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


    还有,如果你有 arcanist 安装, arc flow 显示了上游依赖项的漂亮依赖树(即:先前通过 arc flow new_branch 或手动通过 git branch --set-upstream-to=upstream_branch )