git log:显示左边的第二个父节点 [英] git log: show second parent on the left
问题描述
$ b
#!/ bin / bash -e
rm -rf示例
git init例子
cd例子
echo 0> file.txt
git add file.txt
git commit -am初始提交
git分支branch1
echo 1> file.txt
git commit -am1(on master)
git branch branch2
git checkout branch1
echo 2> file2.txt
git add file2.txt
git commit -m2(on branch1)
git merge --no-edit master
echo 3> file2.txt
git commit -am3(在branch1上)
git checkout master
echo 4> file.txt
git commit -am4(on master)
git checkout branch1
git merge --no-edit master
echo 5> file2.txt
git commit -am5(在分支1上)
git checkout master
git merge --no-edit --no-ff branch1
git log - graph --oneline
我们创建一个分支 branch1
,从 master
分支中合并它几次,最后合并回到 master
。 p>
git log --oneline --graph
看上去很奇怪。
* 2c3b97a合并分支'branch1'
| \
| * 1d722df 5(在分支1上)
| * 54039b6将分支'master'合并到branch1
|中| \
| | /
| / |
* | d80d82c 4(主人)
| * 2399286 3(在分支1上)
| * 9a0cb38将分支'master'合并到branch1
|中| \
| | /
| / |
* | 31df841 1(主人)
| * 5809072 2(在分支1上)
| /
* 08fc7a6初始提交
如果看起来像这样,图表会更简单:
* 2c3b97a合并分支'branch1'
| \
| * 1d722df 5(在分支1上)
| * 54039b6将分支'master'合并到branch1
| / |中
* | d80d82c 4(主人)
| * 2399286 3(在分支1上)
| * 9a0cb38将分支'master'合并到branch1
| / |中
* | 31df841 1(主人)
| * 5809072 2(在分支1上)
| /
* 08fc7a6初始提交
正如我所理解的,它看起来不是这样的原因是, git log
总是倾向于在右侧显示每个合并提交的第二个父所以对于每个合并提交,它都必须向右边绕。 (第二个父级是合并的分支;第一个父级是我们合并的分支。)
- 有一种方法可以说服
git log
来生成我更喜欢的更简单的图表。有一个#!/ usr / bin / python
导入子流程,sys,re
args = ['git','log ','--pretty = format:%x00%H%x01%h%x01%P%x00'] + sys.argv [1:]
rows = subprocess.check_output(args)。 split('\x00')[1 :: 2]
seen_commits = set([row.split('\x01')[0] for row in rows])
print(digraph G {)
用于行中的行:
列= row.split('\x01')
commit = columns [0]
标签=列[1]
print('{}[label ={}];'。format(commit,label))
列中的父级[2] .split ''):
如果父母==:
继续
如果不是父母在seen_commits中:
继续
print('{} - &格式(commit,parent))
print(})
如果你把
$ PATH
放在git-log-dot
和<$ c $如果你输入git log-dot
,+ git-log-dot git
/ code>。 (这是从git
作为单独脚本分发时的遗留保留,git-checkout
,git-reset
等)
您还需要Graphviz。在macOS上,我使用
可以使用以下命令生成一个PNG文件brew install graphviz
安装了它;在Ubuntu上我相信你可以用sudo apt-get graphviz
来安装它。git log-dot
就像这样:
git log-dot | dot -Tpng -o graph.png
(在上面的示例中,我还使用了
-Grankdir = LR
使图形保持水平,但默认的垂直方向也很好。)Consider this example git repository.
#!/bin/bash -e rm -rf example git init example cd example echo 0 > file.txt git add file.txt git commit -am "initial commit" git branch branch1 echo 1 > file.txt git commit -am "1 (on master)" git branch branch2 git checkout branch1 echo 2 > file2.txt git add file2.txt git commit -m "2 (on branch1)" git merge --no-edit master echo 3 > file2.txt git commit -am "3 (on branch1)" git checkout master echo 4 > file.txt git commit -am "4 (on master)" git checkout branch1 git merge --no-edit master echo 5 > file2.txt git commit -am "5 (on branch1)" git checkout master git merge --no-edit --no-ff branch1 git log --graph --oneline
We create a branch
branch1
, merge up to it from themaster
branch a couple of times, and finally merge back tomaster
.git log --oneline --graph
looks weirdly twisted.* 2c3b97a Merge branch 'branch1' |\ | * 1d722df 5 (on branch1) | * 54039b6 Merge branch 'master' into branch1 | |\ | |/ |/| * | d80d82c 4 (on master) | * 2399286 3 (on branch1) | * 9a0cb38 Merge branch 'master' into branch1 | |\ | |/ |/| * | 31df841 1 (on master) | * 5809072 2 (on branch1) |/ * 08fc7a6 initial commit
The graph would be simpler if it looked like this:
* 2c3b97a Merge branch 'branch1' |\ | * 1d722df 5 (on branch1) | * 54039b6 Merge branch 'master' into branch1 |/| * | d80d82c 4 (on master) | * 2399286 3 (on branch1) | * 9a0cb38 Merge branch 'master' into branch1 |/| * | 31df841 1 (on master) | * 5809072 2 (on branch1) |/ * 08fc7a6 initial commit
The reason it doesn't look like that, as I understand it, is that
git log
always prefers to show the "second parent" of each merge commit on the right-hand side, so for each merge commit it has to snake around to the right. (The "second parent" is the branch merging in; the "first parent" is the branch we're merging into.)- Is there a way to convince
git log
to generate the simpler graph I'd prefer? There's an old gist from 2013 proposing fixing this ("Let me specify which branches sink to the left"; there are other interesting ideas there, too), but I see no progress on that. - Assuming
git log
can't do it naturally, is there another git commit graphing tool/library I can use to make a graph like this?
解决方案Thanks to torek's suggestion to try
dot
(aka Graphviz) I wrote this Python script to generate output in thedot
file format. It generates a graph like this, which looks pretty good to me:#!/usr/bin/python import subprocess, sys, re args = ['git', 'log', '--pretty=format:%x00%H%x01%h%x01%P%x00'] + sys.argv[1:] rows = subprocess.check_output(args).split('\x00')[1::2] seen_commits = set([row.split('\x01')[0] for row in rows]) print("digraph G {") for row in rows: columns = row.split('\x01') commit = columns[0] label = columns[1] print('"{}" [label="{}"];'.format(commit, label)) for parent in columns[2].split(' '): if parent == "": continue if not parent in seen_commits: continue print('"{}" -> "{}"'.format(commit, parent)) print("}")
If you put that file on your
$PATH
asgit-log-dot
andchmod +x git-log-dot
,git
will automatically run it if you typegit log-dot
. (It's a legacy holdover from whengit
was distributed as separate scripts,git-checkout
,git-reset
, etc.)You'll also need Graphviz. On macOS I installed it with
brew install graphviz
; on Ubuntu I believe you can install it withsudo apt-get graphviz
.You can generate a PNG file using
git log-dot
like so:git log-dot | dot -Tpng -o graph.png
(In the sample above, I also used
-Grankdir=LR
to make the graph horizontal, but the default vertical orientation is pretty good, too.)这篇关于git log:显示左边的第二个父节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Is there a way to convince