Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查git分支是否有跟踪分支?_Git_Bash - Fatal编程技术网

如何检查git分支是否有跟踪分支?

如何检查git分支是否有跟踪分支?,git,bash,Git,Bash,我正在为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> <

我正在为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
如果存在跟踪分支,它会完全满足我的需要,并输出以下内容:

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--获取branch.master.merge
参考/主管/主管

$git config--get branch.foo.merge#是的,这似乎是找到它的好方法。我很好奇为什么
revparse
会抛出错误。
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"