Output of git branch in tree like fashion
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
|-开发者_JAVA技巧- 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.]
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
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.
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:
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:
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:
- What's the difference between `arc graft` and `arc patch`?
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'
You can use a tool called gitk
.
For those who use Github, they have a branch network viewer that seems easier to read
精彩评论