Showing branch hierarchy at the command line?

Git

Git Problem Overview


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
joes_work
refactoring
experiment

You see output like this:

* master
	joes_work
	refactoring
		experiment

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.

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

Try

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

Solution 3 - Git

I want to complete the answer of @ctcherry.

I like when I can also see the user who did the commit and the date, so this is the following line to use :

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

However this is a pretty long line and difficult to memorize so you can use an alias. You just have to use this in your terminal :

git config --global alias.lg "HERE GOES MY BIG LOG COMMAND LINE"


To summarize copy and paste the line below on your terminal:

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

Then you will just have to use git lg to get your log history tree.

Example: enter image description here

src

Solution 4 - Git

Just type gitk command and press enter.

For me gitk is the easiest solution for this. Though it will not show you command mode, it will automatically populate a nice UI like this :

enter image description here

Solution 5 - Git

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).

Solution 6 - Git

How about this alias for your .gitconfig:

[alias]
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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmellowsoonView Question on Stackoverflow
Solution 1 - GitctcherryView Answer on Stackoverflow
Solution 2 - GitseheView Answer on Stackoverflow
Solution 3 - GitCaribouflexView Answer on Stackoverflow
Solution 4 - GitArifView Answer on Stackoverflow
Solution 5 - GitsvickView Answer on Stackoverflow
Solution 6 - GitPeter EisentrautView Answer on Stackoverflow