参见: 如何查看哪个Git分支正在跟踪哪个远程/上游分支?

如何知道本地分支正在跟踪哪个远程分支?

我是否需要解析git配置输出,或者是否有一个命令可以为我做这件事?


当前回答

我用这个别名

git config --global alias.track '!sh -c "
if [ \$# -eq 2 ]
 then
   echo \"Setting tracking for branch \" \$1 \" -> \" \$2;
   git branch --set-upstream \$1 \$2;
 else
   git for-each-ref --format=\"local: %(refname:short) <--sync--> remote: %(upstream:short)\" refs/heads && echo --URLs && git remote -v;
fi  
" -'

then

git track

注意,该脚本还可以用于设置跟踪。

更多很棒的别名请访问https://github.com/orefalo/bash-profiles

其他回答

这将显示你所在的分支:

$ git branch -vv

这将只显示你所在的当前分支:

$ git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD)

例如:

myremote/mybranch

您可以找到当前分支所使用的远程URL:

$ git remote get-url $(git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD)|cut -d/ -f1)

例如:

https://github.com/someone/somerepo.git
git branch -r -vv

将列出所有分支,包括远程分支。

我不知道这是否算作解析git配置的输出,但这将确定master正在跟踪的远程服务器的URL:

$ git config remote.$(git config branch.master.remote).url

还有另一种方法

git status -b --porcelain

这会给你

## BRANCH(...REMOTE)
modified and untracked files

另一种简单的方法是使用

Cat .git/config在一个git回购

这将列出本地分支的详细信息