首页 > 开发 > 综合 > 正文

Git获取本地分支对应的远端服务器分支名的方法

2020-10-08 16:10:43
字体:
来源:转载
供稿:网友

可以使用下面命令查看本地分支在远端服务器的分支名:

$ 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_UBranch 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 有如下说明:

<branchname>@{upstream}, e.g. master@{upstream}, @{u}
The suffix @{upstream} to a branchname (short form <branchname>@{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脚本中自动获取到远端服务器分支名,而不是只能用默认值、或者要手动输入分支名,方便自动化处理。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持错新网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表