Showing branch hierarchy at the command line?

I’m curious if there is a way to show branch hierarchy on the command line? For instance if I use git branch, instead of seeing output like this:

* master

You see output like this:

  • How can I attach multiple urls to a single git remote?
  • How can I get push user information in server side git hook?
  • Why might AWS Elastic Beanstalk continue serving an old app version?
  • VCS and single developer “team”
  • Can't git aws.push to elastic beanstalk with ruby repo
  • How do I track local-only changes/change sets with git-svn?
  • * master

    That way it’s easy to see which branch a particular branch.. branched off of. Even if there’s no specific command that outputs a tree structure, is there a command that outputs information on which branch came from which branch? I can use a perl script to format the output.

  • What are git branches used for?
  • Jenkins: Retrieving submodules with Git
  • How to make a pull request for just a single change?
  • How to go back a few commits, but still keep commit history in git?
  • Include CocoaPods in version control checkin?
  • Add .gitignore to gitignore
  • 4 Solutions collect form web for “Showing branch hierarchy at the command line?”

    sehe’s solution looks great, here is another one that seems to contain similar information, formatted differently, it uses git log, so it contains commit information as well (ignore the branch names, I kind of messed them up!):

    git log --all --graph --decorate --oneline --simplify-by-decoration
    * ae038ad (HEAD, branch2-1) add content to tmp1
    | * f5a0029 (branch2-1-1) Add another
    * 3e56666 (branch1) Second wave of commits
    | * 6c9af2a (branch1-2) add thing
    * bfcf30a (master) commit 1


    git show-branch
    git show-branch --all

    Example output:

    bash$ git show-branch --all
    ! [branchA] commitA only in branchA
     * [branchB] commitB
      ! [branchC] commitC only in branchC
    +   [branchA] commitA only in branchA 
     *  [branchB] commitB
      + [branchC] commitC only in branchC
     *+ [branchC~1] commitB-1 also in branchC
     *+ [branchC~2] commitB-2 also in branchC
    +++ [branchC~3] common ancestor
    +++ [branchC~4] more common ancestors

    That’s not how branches work from git’s point of view. If I make some commits to branch a, create branch b from it, work there, and then do other work back on a:

    A -- B -- D <-- a
              C <-- b

    That’s indistinguishable if you did it the other way around:

    A -- B -- C <-- b
              D <-- a

    The only way I can think of to find out from which branch certain branch originated is the reflog, but that’s unreliable (entries older than 90 days are usually deleted).

    How about this alias for your .gitconfig:

    branch-tree = !cd "$(git rev-parse --git-dir)/refs/heads" && tree

    You can also give options, depending on what your tree command supports, such as -D for timestamps.

    Git Baby is a git and github fan, let's start git clone.