我对git
没有深入的了解,但在git
中通常会有{remote}/HEAD
,例如origin/HEAD
。以下是来自git remote
的手册页面摘录:
set-head
Sets or deletes the default branch (i.e. the target of the
symbolic-ref refs/remotes/<name>/HEAD) for the named remote.
Having a default branch for a remote is not required, but allows
the name of the remote to be specified in lieu of a specific
branch. For example, if the default branch for origin is set to
master, then origin may be specified wherever you would normally
specify origin/master.
我理解,{remote}/HEAD
是 {remote}
的主/默认分支。可以使用以下命令获取分支名称(有人知道更好的/管道命令吗?):
git branch -r | grep -Po 'HEAD -> \K.*$'
origin/master
当一个人想要获取本地主/默认分支时,通常没有 HEAD
分支,但通常有一个唯一的分支跟踪 {remote}/HEAD
,我们可以使用以下命令获取其名称(同样,肯定有更好的命令):
git branch -vv | grep -Po "^[\s\*]*\K[^\s]*(?=.*$(git branch -rl '*/HEAD' | grep -o '[^ ]\+$'))"
master
当然,如果还没有设置,则需要使用
set-head
命令,以便在
git remote -r
的输出中拥有一个“
HEAD
/master/main分支”。因此,在使用上述命令之前,我们需要运行以下命令一次(感谢@pixelbrackets指出这一点)。
git remote set-head origin -a
更新:
最近,我想获取一些更多的信息(从这里得到一些命令):
git rev-parse --abbrev-ref HEAD
git for-each-ref --format='%(upstream:short)' "$(git symbolic-ref -q HEAD)"
git branch -vv | grep -Po \
"^[\s\*]*\K[^\s]*(?=.*$(git branch -rl '*/HEAD' | grep -o '[^ ]\+$'))"
git branch -r | grep -Po 'HEAD -> \K.*$'