2025-02-24 08:00:01

以树形输出git分支

现在,当我输入"git branch"

它以任意顺序列出我的分支。

我更喜欢的是,如果“git分支”列出了我的输出树一样的时尚,一些像:

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

在这里,foo和bar是从master衍生出来的;Foo1和foo2是foo的分支;Bar4是bar的分支。

这容易做到吗?

[仅限命令行实用程序。这需要适合我的zsh/vim工作流程。


当前回答

下面的答案使用git log:

我在2009年提到过一个类似的方法“无法在终端中显示Git树”:

git log --graph --pretty=oneline --abbrev-commit

但我一直在使用的完整的一个是在“如何使用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

原答案(2010)

Git show-branch -list接近你要找的东西(有拓扑顺序)

--topo-order

默认情况下,分支及其提交是按时间倒序显示的。 这个选项使它们以拓扑顺序出现(即,后代提交显示在它们的父提交之前)。

但是工具git wtf也可以提供帮助。例子:

$ 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告诉你: 如果你的分支是一个跟踪分支,那么你的分支与远程回购是如何关联的。 你的分支如何与非特性(“版本”)分支相关联,如果它是一个特性分支的话。 如果是版本分支,你的分支与特性分支是如何关联的

其他回答

对于那些使用Github的人来说,他们有一个分支网络查看器,看起来更容易阅读

下面的答案使用git log:

我在2009年提到过一个类似的方法“无法在终端中显示Git树”:

git log --graph --pretty=oneline --abbrev-commit

但我一直在使用的完整的一个是在“如何使用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

原答案(2010)

Git show-branch -list接近你要找的东西(有拓扑顺序)

--topo-order

默认情况下,分支及其提交是按时间倒序显示的。 这个选项使它们以拓扑顺序出现(即,后代提交显示在它们的父提交之前)。

但是工具git wtf也可以提供帮助。例子:

$ 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告诉你: 如果你的分支是一个跟踪分支,那么你的分支与远程回购是如何关联的。 你的分支如何与非特性(“版本”)分支相关联,如果它是一个特性分支的话。 如果是版本分支,你的分支与特性分支是如何关联的

你可以使用一个叫做gitk的工具。

我做了一个简单的CLI,它完全做到了这一点,并将其用于酿造。

虽然不是你想要的,但是

git log --graph --simplify-by-decoration --pretty=format:'%d' --all

做得很好。它还显示标签和远程分支。这可能不是每个人都想要的,但我发现它很有用。通过装饰来简化是这里限制引用的一个大技巧。

我使用类似的命令来查看我的日志。我已经能够完全取代我的gitk用法与它:

git log --graph --oneline --decorate --all

我通过在~/中包含这些别名来使用它。gitconfig文件:

[alias]
    l = log --graph --oneline --decorate
    ll = log --graph --oneline --decorate --branches --tags
    lll = log --graph --oneline --decorate --all

编辑:更新了建议的日志命令/别名,以使用更简单的选项标志。