rev-parse和symbolic-ref的输出之间的区别是什么?

时间:2017-07-11 07:31:49

标签: git

我已经通过两种不同的方式让git告诉我当前的分支:

git rev-parse --abbrev-ref HEAD

git symbolic-ref --short HEAD

呃......两者究竟做了什么,何时会有所不同,如果有的话?

1 个答案:

答案 0 :(得分:11)

它们在分离的HEAD状态下(当没有当前的命名分支时)表现不同。在这种情况下,HEAD不是符号引用,git symbolic-ref退出时会出错,而git rev-parse --abbrev-ref会将HEAD解析为自身。

$ git checkout HEAD^
Note: checking out 'HEAD^'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at bb2d3b1... fix layout with extra newline

$ git symbolic-ref --short HEAD
fatal: ref HEAD is not a symbolic ref

$ git rev-parse --abbrev-ref HEAD
HEAD

您可能还想查看git命令name-rev。 git尽力确定你所在的分支,即使处于分离状态

$ git name-rev --name-only HEAD
master~1