我已经遇到了 git 告诉我当前分支的 2 种不同方式:
git rev-parse --abbrev-ref HEAD
和
git symbolic-ref --short HEAD
呃......两者究竟做了什么,什么时候会有所不同,如果有的话?
它们在分离的 HEAD 状态下的行为不同(当没有当前命名分支时(。在这种情况下,HEAD
不是符号 ref,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