Output of git branch in tree like fashion

前端 未结 6 857
野的像风
野的像风 2020-12-04 04:34

Right now, when I type \"git branch\"

it lists my branches in an arbitrary order.

What I would prefer would be if \"git branch\" listed my output in a tree l

相关标签:
6条回答
  • 2020-12-04 05:07

    The answer below uses git log:

    I mentioned a similar approach in 2009 with "Unable to show a Git tree in terminal":

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

    But the full one I have been using is in "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
    

    Original answer (2010)

    git show-branch --list comes close of what you are looking for (with the topo order)

    --topo-order
    

    By default, the branches and their commits are shown in reverse chronological order.
    This option makes them appear in topological order (i.e., descendant commits are shown before their parents).

    But the tool git wtf can help too. Example:

    $ 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 shows you:

    • How your branch relates to the remote repo, if it's a tracking branch.
    • How your branch relates to non-feature ("version") branches, if it's a feature branch.
    • How your branch relates to the feature branches, if it's a version branch
    0 讨论(0)
  • 2020-12-04 05:10

    Tested on Ubuntu:

    sudo apt install git-extras
    git-show-tree
    

    This produces an effect similar to the 2 most upvoted answers here.

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


    Also, if you have arcanist installed (correction: Uber's fork of arcanist installed--see the bottom of this answer here for installation instructions), arc flow shows a beautiful dependency tree of upstream dependencies (ie: which were set previously via arc flow new_branch or manually via git branch --set-upstream-to=upstream_branch).

    Bonus git tricks:

    • How do I know if I'm running a nested shell? - see section here titled "Bonus: always show in your terminal your current git branch you are on too!"

    Related:

    1. What's the difference between `arc graft` and `arc patch`?
    0 讨论(0)
  • 2020-12-04 05:14

    You can use a tool called gitk.

    0 讨论(0)
  • 2020-12-04 05:29

    For those who use Github, they have a branch network viewer that seems easier to read

    0 讨论(0)
  • 2020-12-04 05:30

    The following example shows commit parents as well:

    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'
    
    0 讨论(0)
  • 2020-12-04 05:31

    It's not quite what you asked for, but

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

    does a pretty good job. It shows tags and remote branches as well. This may not be desirable for everyone, but I find it useful. --simplifiy-by-decoration is the big trick here for limiting the refs shown.

    I use a similar command to view my log. I've been able to completely replace my gitk usage with it:

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

    I use it by including these aliases in my ~/.gitconfig file:

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

    Edit: Updated suggested log command/aliases to use simpler option flags.

    0 讨论(0)
提交回复
热议问题