这篇文章主要介绍了Git获取本地分支对应的远端服务器分支名的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
可以使用下面命令查看本地分支在远端服务器的分支名:
$ git rev-parse --abbrev-ref local_branch_name@{upstream}
把 local_branch_name 换成要查询的本地分支名,例如 master 等。下面通过例子来说明这个命令各个参数的含义。
先创建一个新的本地分支,名为 new_local_branch,关连到远端服务器的 Remote_Branch_U 分支:
$ git checkout -b new_local_branch aosp/Remote_Branch_U Branch new_local_branch set up to track remote branch Remote_Branch_U from aosp. Switched to a new branch 'new_local_branch'
查看本地分支 new_local_branch 在远端服务器的分支名:
$ git rev-parse --abbrev-ref new_local_branch@{upstream} aosp/Remote_Branch_U
如果所给的本地分支名没有关连到远端服务器分支,会打印报错信息:
$ git rev-parse --abbrev-ref great@{upstream} fatal: No upstream configured for branch 'great'
注意:@{upstream} 这一整串本身是命令的一部分,直接输入即可,不是要把 upstream 或者
{upstream} 替换成远端服务器仓库名。查看 man git-rev-parse 有如下说明:
@{upstream}, e.g. master@{upstream}, @{u}
The suffix @{upstream} to a branchname (short form@{u}) refers to the branch that the branch specified by branchname is set to build on top of. A missing branchname defaults to the current one.
即,@{upstream} 可以缩写为 @{u}。如果不提供分支名,默认用当前本地分支名。
另外,如果不加 --abbrev-ref 选项,会打印分支head的hash值,而不是打印分支名。
$ git rev-parse --abbrev-ref new_local_branch@{u} aosp/Remote_Branch_U $ git rev-parse --abbrev-ref @{u} aosp/Remote_Branch_U $ git rev-parse @{u} 66355f171f5ba7dbc66465e761b97afe2395b06e
这个命令可在shell脚本中自动获取到远端服务器分支名,而不是只能用默认值、或者要手动输入分支名,方便自动化处理。
以上就是Git获取本地分支对应的远端服务器分支名的方法的详细内容,更多请关注0133技术站其它相关文章!