225

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 like fasion, somethign like:

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

Where here, foo & bar were branched from master; foo1 & foo2 were branched from foo; bar4 was branched from bar.

Is this easy to accomplish?

[Command line utilities only. This needs to fit into my zsh/vim workflow.]

anon
  • 41,035
  • 53
  • 197
  • 293
  • None of the answers here (my own included) seem to provide an adequate solution for what I think you really want, and for what I know I want. I'm going to write a new utility to solve this when I get the chance. Will probably call it `git_tree`. It will output something like `arc flow` does here: https://stackoverflow.com/questions/54227968/whats-the-difference-between-arc-graft-and-arc-patch/54231342#54231342. Perhaps someday I can even get it merged into git itself. – Gabriel Staples Mar 19 '20 at 22:18
  • This person seems to want the same thing too: https://www.reddit.com/r/git/comments/282c1f/how_to_see_branch_tree_from_command_line/ – Gabriel Staples Mar 19 '20 at 23:15
  • `git log --graph` is enough, I think. – DawnSong Jul 17 '20 at 12:36

7 Answers7

292

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
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Been using a variation of your pretty format that shows the author email as well, using %ae. Also I prefer to call the alias by "sl" to resemble hg's smartlog. – fiorix Dec 16 '17 at 09:40
  • Thanks a lot for the link to the `git-wtf` tool, it's insanely useful. Seems to essentially break down the conclusions I would draw from staring at a fancy `git log` tree, but in a nice summary. – Luke Davis Feb 18 '18 at 19:50
  • I was hoping to find out a way so that checkout branch should show from which branch it was checked out from – Chang Zhao Jul 23 '19 at 16:01
  • @ChangZhao That is similar to "finding the parent branch", and that is not easy to do: https://stackoverflow.com/a/3162929/6309, https://stackoverflow.com/a/56452713/6309 – VonC Jul 23 '19 at 16:05
  • WTF there is no `git wtf` in my git (version 2.25.1) – jarno Apr 10 '21 at 07:57
  • 1
    @jarno That is expected: `git-wtf` is an executable (ruby script) that you should put in your $PATH in order to be able to execute `git wtf` – VonC Apr 10 '21 at 10:38
175

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.

nocash
  • 3,687
  • 4
  • 19
  • 12
  • 1
    IMO this is the best answer here, but I think [SourceTree](http://sourcetreeapp.com/) or gitk or the like is the way to go for this kind of thing. – JaKXz Sep 03 '14 at 21:23
  • This displays the branches at the origin. Is there any way to get this to display for the local branches? – Jeff Jan 13 '15 at 16:05
  • @Jeff replacing `--all` with `--branches --tags` would probably do it. – nocash Feb 20 '15 at 22:03
17

TLDR; use git show-tree, or the git lg alias (my preferred choice).

1. To show all branches (including remote branches) in a tree view:

Use git show-tree:

Note: git show-tree is not part of core git. You have to install it as an extra utility, known as "git-extras". On Linux Ubuntu, I do that with sudo apt install git-extras. Find your operating system in the installation instructions here and follow the instructions to install it: Installing git-extras.

Tested on Ubuntu:

# Install it
sudo apt install git-extras

# Run it:
git-show-tree
# OR (same thing)
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

Examples:

man git show-tree shows the following example:

EXAMPLES

Output the commit history log for all branches as tree view:

*   4b57684 (HEAD, develop) Merge branch upstream master.
|\
| *   515e94a Merge pull request #128 from nickl-/git-extras-html-hyperlinks
| |\
| | * 815db8b (nickl/git-extras-html-hyperlinks, git-extras-html-hyperlinks) help ronn make hyperlinks.
| * | 7398d10 (nickl/develop) Fix #127 git-ignore won´t add duplicates.
| |/
| | * ab72c1e (refs/stash) WIP on develop: 5e943f5 Fix #127 git-ignore won´t add duplicates.
| |/
|/|
* | 730ca89 (bolshakov) Rebase bolshakov with master
|/
* 60f8371 (origin/master, origin/HEAD, master) Merge pull request #126 from agrimaldi/fix-changelog-last-tag
* 9627780 (tag: 1.7.0) Release 1.7.0
* 2e53ff6 (tag: 1.6.0) Release 1.6.0
* bbd32d8 (tag: 1.5.1) Release 1.5.1
| * 6b6b758 (nickl/gh-pages, gh-pages) add example git-extras to gh-pages
| * 19cfd11 (origin/gh-pages) Index page
| | * 881a70e (tag: 1.5.0) Release 1.5.0
| |/
|/|
* | 4db5ee0 (tag: 1.4.0) Release 1.4.0
* | 9b0bc89 (tag: 1.3.0) Release 1.3.0
* | be49961 (tag: 1.2.0) Release 1.2.0
* | c1d2dfc (tag: 1.1.0) Release 1.1.0
* | 4a56adb (tag: 1.0.0) Release 1.0.0
* | 948308b (tag: 0.9.0) Release 0.9.0
* | 40b131d (tag: 0.8.1) Release 0.8.1
* | 391431d (tag: 0.8.0) Release 0.8.0

And here is a sample output of git show-tree on the ardupilot repo:

enter image description here


Also, if you have arcanist installed (correction: Uber's fork of arcanist installed--see the bottom of my 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). Example output of arc flow:

master                              
└──graft-D999
   └──new_feature_1
      └──new_feature_2 

I really find the arc flow view beautiful and useful, so I have this (inactive, and not-yet-functional) git tree open-source project where I plan to duplicate its behavior: https://github.com/ElectricRCAircraftGuy/git-tree.

2. To show one branch in a "tree-like" fashion:

# Show the currently-checked-out branch
git lg

# OR: show branch_name
git lg branch_name

You can add -p or --patch to show the lines changed too:

git lg -p
# OR
git lg --patch

To install the git lg alias (source: https://coderwall.com/p/euwpig/a-better-git-log):

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

Example output showing forks and merges and stuff on the sshfs repo:

enter image description here

Bonus git tricks:

Related:

  1. What's the difference between `arc graft` and `arc patch`?
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • Which version of `git` has `show-tree`? Mine doesn't have it. – James Hirschorn Aug 06 '22 at 16:54
  • 1
    @JamesHirschorn, it's not part of core `git`. You have to install it as an extra utility. On Linux Ubuntu, I do that with `sudo apt install git-extras`. Find your operating system in the installation instructions here and follow the instructions: https://github.com/tj/git-extras/blob/master/Installation.md#installing-git-extras. I've updated my answer with this information. – Gabriel Staples Aug 06 '22 at 16:59
  • can `git show-tree` ignore tags? – alper Feb 15 '23 at 16:24
  • @alper, I don't know what that means. What do you mean by "ignore tags"? It follows commits. – Gabriel Staples Feb 15 '23 at 17:21
  • @GabrielStaples `(tag:` ... `git-show-tree | grep -Ev "Initial commit|tag: " | grep -v '|'` – alper Feb 16 '23 at 13:35
12

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'
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Ben
  • 191
  • 2
  • 4
10

You can use a tool called gitk.

Vladimir Prudnikov
  • 6,974
  • 4
  • 48
  • 57
  • I love gitk, but I didn't figure out ```gitk``` in Mac. If you have any suggestion, please let me know. I started using ```Github Desktop``` but love to work on command line. – AMIC MING Jul 13 '16 at 18:44
0

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

kip2
  • 6,473
  • 4
  • 55
  • 72
0

I made a simple CLI that exactly does that and put it to brew.

parvula
  • 31
  • 5