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 joes_work refactoring experiment
* 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.
The question has nothing to do with the relationship between commits, branches only. You can have 5 branches pointed to unrelated commits which nevertheless form a graph in terms of their upstream branch. I don’t think there’s any way to get the answer from the git cli.
7 Answers 7
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
Yeah — I actually prefer this because it’s more flexible but typing it from my phone would be a pain — plus most likely it would be a bit overkill to the OP
@sehe: you can make an alias of it. I have a similar git log alias that’s reeeaaally nice, and I alias it to lg, so git lg.
Thanks ctcherry, this is really useful (second time I’ve needed it). I’d give you another up vote if I could.
Nice! I use git config —global alias.tree ‘log —all —graph —decorate —oneline —simplify-by-decoration’ which gives me git tree .
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)%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)%Creset' --abbrev-commit"
Then you will just have to use git lg to get your log history tree.
Источник
Посмотреть дерево веток git
Changes in the git-ls-tree manual
- 2.36.1 → 2.41.0 no changes
- 2.36.0
04/18/22
- 2.30.1 → 2.35.8 no changes
- 2.30.0
12/27/20
Check your version of git by running
NAME
git-ls-tree — List the contents of a tree object
SYNOPSIS
git ls-tree [-d] [-r] [-t] [-l] [-z] [--name-only] [--name-status] [--object-only] [--full-name] [--full-tree] [--abbrev[=]] [--format=] […]
DESCRIPTION
Lists the contents of a given tree object, like what «/bin/ls -a» does in the current working directory. Note that:
- the behaviour is slightly different from that of «/bin/ls» in that the denotes just a list of patterns to match, e.g. so specifying directory name (without -r ) will behave differently, and order of the arguments does not matter.
- the behaviour is similar to that of «/bin/ls» in that the is taken as relative to the current working directory. E.g. when you are in a directory sub that has a directory dir, you can run git ls-tree -r HEAD dir to list the contents of the tree (that is sub/dir in HEAD ). You don’t want to give a tree that is not at the root level (e.g. git ls-tree -r HEAD:sub dir ) in this case, as that would result in asking for sub/sub/dir in the HEAD commit. However, the current working directory can be ignored by passing —full-tree option.
OPTIONS
Show only the named tree entry itself, not its children.
Show tree entries even when going to recurse them. Has no effect if -r was not passed. -d implies -t .
Show object size of blob (file) entries.
\0 line termination on output and do not quote filenames. See OUTPUT FORMAT below for more information.
List only filenames (instead of the «long» output), one per line. Cannot be combined with —object-only .
List only names of the objects, one per line. Cannot be combined with —name-only or —name-status . This is equivalent to specifying —format=’%(objectname)’ , but for both this option and that exact format the command takes a hand-optimized codepath instead of going through the generic formatting mechanism.
Instead of showing the full 40-byte hexadecimal object lines, show the shortest prefix that is at least hexdigits long that uniquely refers the object. Non default number of digits can be specified with —abbrev=.
Instead of showing the path names relative to the current working directory, show the full path names.
Do not limit the listing to the current working directory. Implies —full-name.
A string that interpolates %(fieldname) from the result being shown. It also interpolates %% to % , and %xx where xx are hex digits interpolates to character with hex code xx ; for example %00 interpolates to \0 (NUL), %09 to \t (TAB) and %0a to \n (LF). When specified, —format cannot be combined with other format-altering options, including —long , —name-only and —object-only .
When paths are given, show them (note that this isn’t really raw pathnames, but rather a list of patterns to match). Otherwise implicitly uses the root level of the tree as the sole path argument.
Output Format
The output format of ls-tree is determined by either the —format option, or other format-altering options such as —name-only etc. (see —format above).
The use of certain —format directives is equivalent to using those options, but invoking the full formatting machinery can be slower than using an appropriate formatting option.
In cases where the —format would exactly map to an existing option ls-tree will use the appropriate faster path. Thus the default format is equivalent to:
%(objectmode) %(objecttype) %(objectname)%x09%(path)
This output format is compatible with what —index-info —stdin of git update-index expects.
When the -l option is used, format changes to
%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)
Object size identified by is given in bytes, and right-justified with minimum width of 7 characters. Object size is given only for blobs (file) entries; for other entries — character is used in place of size.
Without the -z option, pathnames with «unusual» characters are quoted as explained for the configuration variable core.quotePath (see git-config[1]). Using -z the filename is output verbatim and the line is terminated by a NUL byte.
It is possible to print in a custom format by using the —format option, which is able to interpolate different fields using a %(fieldname) notation. For example, if you only care about the «objectname» and «path» fields, you can execute with a specific «—format» like
git ls-tree —format=’%(objectname) %(path)’
FIELD NAMES
Various values from structured fields can be used to interpolate into the resulting output. For each outputing line, the following names can be used:
The type of the object ( commit , blob or tree ).
The size of a blob object («-» if it’s a commit or tree ). It also supports a padded format of size with «%(objectsize:padded)».
The pathname of the object.
Источник
Viewing full version tree in git
I am using the command line version of Git and gitk. I want to see the full version tree, not just the part that is reachable from the currently checked out version. Is it possible?
6 Answers 6
if you happen to not have a graphical interface available you can also print out the commit graph on the command line:
git log --oneline --graph --decorate --all
if this command complains with an invalid option —oneline, use:
git log --pretty=oneline --graph --decorate --all
I have hope command line abbreviations were invented before tab completion. They only benefit those who use those commands a lot and those with crazy memories.
- When I’m in my work place with terminal only, I use: git log —oneline —graph —color —all —decorate
- When the OS support GUI, I use: gitk —all
- When I’m in my home Windows PC, I use my own GitVersionTree
Perfect answer for me. My OS supports GUI so second option is my way to go but let`s say I just wanna peek the graph from command line very quickly : is there some way to avoid typing all of those switches from the first version, or you just re-type them all the time ? Thank you.
@rchrd I would set them as alias by running git config —global alias.ver «log —oneline —graph —color —all —decorate» and only need to type git ver thereafter.
You can try the following:
You can tell gitk what to display using anything that git rev-list understands, so if you just want a few branches, you can do:
gitk master origin/master origin/experiment
. or more exotic things like:
gitk --simplify-by-decoration --all
There is a very good answer to the same question.
Adding following lines to «~/.gitconfig»:
[alias] lg1 = log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all lg = !"git lg1"
Источник