[英]How do I check if git branch has a tracking branch?
我正在为bash编写一个小git帮助器,我需要知道某个名称的分支是否有跟踪分支。
更具体地说,问题是如果你在一个没有跟踪分支的分支上运行git pull
,它将失败,并带有以下内容:
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> foo
git pull --quiet
也不会压制此消息。
我设法找到了这个有用的快捷方式:
git rev-parse --symbolic --abbrev-ref foo@{u}
它完全符合我的需要,如果存在跟踪分支,则输出以下内容:
origin/foo
但是如果一个分支没有跟踪分支,那么这是输出:
fatal: No upstream configured for branch 'foo'
这是相当不错的,除了它存在非零状态,并将其输出到stderr。
所以我基本上想做的是:
tracking_branch=$(git do-some-magick foo)
if [[ -n $tracking_branch ]]; then
git pull
fi
而不是这个:
tracking_branch=$(git rev-parse --symbolic --abbrev-ref foo@{u} 2> /dev/null)
if [[ -n $tracking_branch ]]; then
git pull
fi
它实际上工作正常,但对我来说似乎不对。 有没有其他方法可以做到这一点?
您可以尝试这样找到跟踪分支:
git config --get branch.foo.merge
$ git config --get branch.master.merge
refs/heads/master
$ git config --get branch.foo.merge # <- nothing printed for non-tracked branch "foo"
根据git pull手册,有关跟踪分支的信息存储在特定于存储库的.git/config
:
<repository>和<branch>的默认值是从git-branch [1] --track设置的当前分支的“remote”和“merge”配置中读取的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.