git - git log --decorate : (HEAD -> master) vs (HEAD, master 的输出差异)

标签 git git-log

当我获得 GIT 存储库的日志时:

git log --oneline --decorate --graph

输出是这样的:

* 44025ed (HEAD -> master) second commit
* adf2dbb first commmit

在另一个 repo 中,当我 git log 时,我得到:

* 435b61d (HEAD,master) bar
* 9773e52 foo

(HEAD -> master)(HEAD,master) 有什么区别?

最佳答案

箭头指向当前分支

HEAD 右侧的箭头,在 git log --oneline --decorate --graph 的输出中,指示哪个分支(如果有)是当前的。

* 44025ed (HEAD -> master) second commit

表示符号引用HEAD当前指向master分支;换句话说,您处于 detached-HEAD 状态,当前分支是 master

enter image description here

相比之下,

* 44025ed (HEAD, master) second commit

表示符号引用 HEAD 当前指向任何分支,而是直接指向提交 (44025ed);换句话说,你处于 detached-HEAD 状态。 master 分支仅与 HEAD 一起列出,因为它恰好指向相同的提交 (44025ed)。

enter image description here

一些历史

有关信息,在我提出以下问题后不久,在 Git (2.4) 中引入了这种区别:Can git log --decorate unambiguously tell me whether the HEAD is detached?

一个小实验(固定想法)

$ mkdir decorate-test
$ cd decorate-test/
$ git init
Initialized empty Git repository in /xxxxxxx/decorate-test/.git/
$ touch README
$ git add README
$ git commit -m "Add README"
[master (root-commit) 50781c9] Add README
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README
$ git log --oneline --decorate --graph
* 50781c9 (HEAD -> master) Add README
# Note the presence of the arrow in the output.

# Now, check out the commit directly to detach the HEAD:
$ git checkout 50781c9
Note: checking out '50781c9'.

You are in 'detached HEAD' state. You can look around, ...

HEAD is now at 50781c9... Add README
$ git log --oneline --decorate --graph
* 50781c9 (HEAD, master) Add README
# The arrow is gone!

# Check out master again to reattach the HEAD:
$ git checkout master
Switched to branch 'master'
$ git log --oneline --decorate --graph
* 50781c9 (HEAD -> master) Add README
# The arrow is back!

关于git - git log --decorate : (HEAD -> master) vs (HEAD, master 的输出差异),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37344763/

相关文章:

git - 没有瓷器的基本 Git?

git - 使用 git hook 添加许可证和应用程序版本以在源文件顶部注释

git - 使用git日志显示 merge 期间更改的文件

Git:相当于 `--full-history` 和 `git bisect` 的 `git blame`

git - 如何在行被删除/删除时找到提交?

git - 可以在 git hook 中自定义 GIT_COMMITTER_DATE 吗?

java - JGit 中的子树推送

git - 如何让 git commit 消息分成多行?

单个修订的 git 日志

git log --before ="4 months"显示 3 周前提交的分支。我究竟做错了什么?