令人困惑的 git 日志结果

confusing git log results

为了获得 git 分支层次结构,我向我的 git 存储库发出以下命令:

git log --all --graph --decorate --oneline --simplify-by-decoration

但是,我得到了一个令人困惑的结果:

* 023448b (I) Comments
| * 7b08b45 (H) Comments
|/  
| * 379334c (G) Comments
| * ec95b66 (F)  Comments
|/  
| * dca9a7b (E) Comments
|/  
| * f7bb48a (D) Comments
|/  
| * 82224b2 (C) Comments
|/  
* c7887dd (HEAD, master, A, B) Commments
* 5046cbf Initial Commit: Comments

最后一行显示了三个分支。不是应该只显示 master 吗?还有最后一句是什么意思?

我不知道你所说的 "the last sentence" 是什么意思,但是:

Isnt it supposed to show only the Master?

没有。你说 --all。 All 表示 all: 所有分支、所有标签以及所有其他引用。如果你只想查看分支 master 你应该说 master.

git log 查找要显示的提交的方式是从一些引用集开始,然后检查每个提交这些引用 point-to,然后查看这些提交的 parent 提交,然后查看 parents' parents,依此类推。 "walk" 通过历史查看每个提交的 parent 或 parent,并将这些提交排队以供进一步检查 - 只需 指定点开始,并继续,直到没有更多的提交。每当此进程到达 root commit 时提交 "run out",这是一个没有 parents 的提交。1

git log 的默认设置是从名称 HEAD 开始,除非您指定其他起点。你做到了:你说 --all,这意味着 所有 个起点(reflogs 除外)。

同时:

--simplify-by-decoration

git log 定向到 跳过 没有某些分支或标记名称指向它们的提交。但是,无论出于何种原因,这从不2 跳过根提交。并且:

--decorate

指示 git log 将指向该提交的任何分支 and/or 标记的名称放在显示的任何提交上(当然是 --simplify-by-decoration 保留的那些,除了根提交)。因此,如果您将 --all 替换为 master,您应该会看到:

* c7887dd (HEAD, master, A, B) Commments
* 5046cbf Initial Commit: Comments

因为名称 HEADmasterAB 都指向 c7887dd(保留是因为名称指向它),并且 5046cbf 是一个根提交(因为它是一个根提交而被保留)。


1请注意,这只会停止向 "commits to examine" 的队列添加 更多 提交。如果队列中已经有许多排队的提交,则该过程会继续检查这些提交。如果图中有多个根提交,这个过程可能会找到几个或全部(取决于你从哪里开始遍历)。

git loggit rev-list 都对图中的提交节点进行 breadth-first 遍历,但都对提交进行 排序 。添加 --graph 强制排序使用 topology-respecting 顺序,其中在显示所有 children 之前不显示 parent 提交(在正常工作时 "backwards" 方向, 即).

2粗略re-check的source code suggests that this is not quite true: root commits will be discarded if they have an empty tree attached. This check happens after modifying the attached tree based on any pathspec arguments given to git log or git rev-list, so you won't see root commits when looking for commits that modified some particular file. In this case there are no pathspecs, so you will only not-see the root commit if it is literally empty (refers to the empty tree).