参见: 如何查看哪个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 branch -a -vv

查看本地分支在哪里显式地配置了推和拉。

git remote show {remote_name}

这将显示你所在的分支:

$ 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

改进这个答案,我想出了这些.gitconfig别名:

branch-name = "symbolic-ref --short HEAD"
branch-remote-fetch = !"branch=$(git branch-name) && git config branch.\"$branch\".remote || echo origin #"
branch-remote-push  = !"branch=$(git branch-name) && git config branch.\"$branch\".pushRemote || git config remote.pushDefault || git branch-remote-fetch #"
branch-url-fetch = !"remote=$(git branch-remote-fetch) && git remote get-url        \"$remote\" #"  # cognizant of insteadOf
branch-url-push  = !"remote=$(git branch-remote-push ) && git remote get-url --push \"$remote\" #"  # cognizant of pushInsteadOf
git branch -vv | grep 'hardcode-branch-name'
# "git rev-parse --abbrev-ref head" will get your current branch name
# $(git rev-parse --abbrev-ref head) save it as string
#  find the tracking branch by grep filtering the current branch 
git branch -vv | grep $(git rev-parse --abbrev-ref head)

您可以使用git签出,即。“签出当前分支”。如果存在当前分支的跟踪信息,这是一个没有副作用的操作。

$ git checkout 
Your branch is up-to-date with 'origin/master'.