我见过一些书籍和文章中有一些非常漂亮的Git分支和提交的图表。如何制作Git历史的高质量可打印图像?


当前回答

我在~/.gitconfig中有这个git日志别名来查看图形历史:

[alias]
l = log --all --graph --pretty=format:'%C(auto)%h%C(auto)%d %s %C(dim white)(%aN, %ar)'

有了这一点,git l将输出如下内容:

在Git2.12+中,您甚至可以使用log.graphColors配置选项自定义图形的线条颜色。

至于日志的格式,它类似于--oneline,添加了作者名(尊重.mailmap)和相对作者日期。请注意,在Git>=1.8.3中支持%C(auto)语法,告诉Git使用提交散列等的默认颜色。

其他回答

我添加了三个自定义命令:gittree、gitstree和gitvtree。我会按顺序检查它们。

[alias]
    tree = log --all --graph --decorate=short --color --format=format:'%C(bold blue)%h%C(reset) %C(auto)%d%C(reset)\n         %C(black)[%cr]%C(reset)  %x09%C(black)%an: %s %C(reset)'


使用gitstree和gitvtree,我使用Bash来帮助格式化。

[alias]
    logx = log --all --graph --decorate=short --color --format=format:'%C(bold blue)%h%C(reset)+%C(dim black)(%cr)%C(reset)+%C(auto)%d%C(reset)++\n+++       %C(bold black)%an%C(reset)%C(black): %s%C(reset)'
    stree = !bash -c '"                                                                             \
        while IFS=+ read -r hash time branch message; do                                            \
            timelength=$(echo \"$time\" | sed -r \"s:[^ ][[]([0-9]{1,2}(;[0-9]{1,2})?)?m::g\");     \
            timelength=$(echo \"16+${#time}-${#timelength}\" | bc);                                 \
            printf \"%${timelength}s    %s %s %s\n\" \"$time\" \"$hash\" \"$branch\" \"\";          \
        done < <(git logx && echo);"'


[alias]
    logx = log --all --graph --decorate=short --color --format=format:'%C(bold blue)%h%C(reset)+%C(dim black)(%cr)%C(reset)+%C(auto)%d%C(reset)++\n+++       %C(bold black)%an%C(reset)%C(black): %s%C(reset)'
    vtree = !bash -c '"                                                                             \
        while IFS=+ read -r hash time branch message; do                                            \
            timelength=$(echo \"$time\" | sed -r \"s:[^ ][[]([0-9]{1,2}(;[0-9]{1,2})?)?m::g\");     \
            timelength=$(echo \"16+${#time}-${#timelength}\" | bc);                                 \
            printf \"%${timelength}s    %s %s %s\n\" \"$time\" \"$hash\" \"$branch\" \"$message\";  \
        done < <(git logx && echo);"'


这适用于Git 1.9a版本。颜色值“auto”显然在这个版本中首次出现。这是一个很好的补充,因为分支名称将获得不同的颜色。例如,这使得区分本地分支和远程分支更加容易。

这里是我的社区别名:gitls。使用gitls,您可以查看图形,每次提交只覆盖一行。一切都是彩色的,并带有有用的信息。您还可以检查其他分支上的提交历史记录:

git config --global alias.ls '!f() { git log $1 --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cgreen\\ [%ae,%ar]" --decorate --graph; }; f'

用法:

# shows commits graph on the current branch
git ls

# shows commits graph on the develop branch
git ls develop 

以下是图表视图:

对于文本输出,您可以尝试:

git log --graph --abbrev-commit --decorate --date=relative --all

Or:

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

或者:这里有一个Graphviz别名,用于绘制DAG图。

我个人使用gitx、gitk-all和gitnub。

要获得更详细的文本输出,请尝试:

git log --graph --date-order -C -M --pretty=format:"<%h> %ad [%an] %Cgreen%d%Creset %s" --all --date=short

您可以在文件$HOME/.gitconfig中添加别名:

[alias]
    graph = log --graph --date-order -C -M --pretty=format:\"<%h> %ad [%an] %Cgreen%d%Creset %s\" --all --date=short

git forest是一个优秀的Perl脚本,我已经使用了一年多,我几乎不再直接使用git log命令。

以下是我喜欢这个剧本的一些地方:

它使用Unicode字符在图形中绘制线条,使图形线条看起来更加连续。您可以将--reverse与图形输出相结合,这在常规gitlog命令中是不可能的。它在内部使用gitlog获取提交列表,因此传递给gitlog的所有选项也可以传递给该脚本。

我有一个使用git forest的别名,如下所示:

[alias]
tree = "forest --pretty=format:\"%C(red)%h %C(magenta)(%ar) %C(blue)%an %C(reset)%s\" --style=15 --reverse"

这是终端输出的外观: