git 在命令行显示分支层次结构?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7623278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:58:34  来源:igfitidea点击:

Showing branch hierarchy at the command line?

git

提问by mellowsoon

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:

我很好奇是否有办法在命令行上显示分支层次结构?例如,如果我使用git branch, 而不是看到这样的输出:

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

这样就很容易看出一个特定的分支是从哪个分支...分支出来的。即使没有输出树结构的特定命令,是否有输出关于哪个分支来自哪个分支的信息的命令?我可以使用 perl 脚本来格式化输出。

回答by ctcherry

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!):

sehe 的解决方案看起来很棒,这是另一个似乎包含类似信息、格式不同的解决方案,它使用 git log,所以它也包含提交信息(忽略分支名称,我有点搞砸了!):

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

回答by sehe

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

回答by Caribouflex

I want to complete the answer of @ctcherry.

我想完成@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"

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 lgto get your log history tree.

然后您只需要使用git lg来获取您的日志历史树。

Example: enter image description here

例子: 在此处输入图片说明

src

源文件

回答by Arif

Just type gitkcommand and press enter.

只需输入gitk命令并按回车键即可。

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

对我gitk来说,这是最简单的解决方案。虽然它不会显示命令模式,但它会自动填充一个漂亮的 UI,如下所示:

enter image description here

在此处输入图片说明

回答by svick

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

从 git 的角度来看,这不是分支的工作方式。如果我对 branch 做了一些提交ab从它创建分支,在那里工作,然后再做其他工作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).

我能想到的找出某个分支源自哪个分支的唯一方法是引用日志,但这是不可靠的(通常会删除超过 90 天的条目)。

回答by Peter Eisentraut

How about this alias for your .gitconfig:

你的这个别名怎么样.gitconfig

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

You can also give options, depending on what your treecommand supports, such as -Dfor timestamps.

您还可以根据您的tree命令支持的内容提供选项,例如-D时间戳。