【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
我知道我可以做git branch --all ,这向我展示了本地和远程分支,但它在向我展示它们之间的关系方面没有用。
如何以显示哪个本地分支正在跟踪哪个远程的方式列出分支?
#1楼
我用这个别名
git config --global alias.track '!f() { ([ $# -eq 2 ] && ( echo "Setting tracking for branch " $1 " -> " $2;git branch --set-upstream $1 $2; ) || ( git for-each-ref --format="local: %(refname:short) <--sync--> remote: %(upstream:short)" refs/heads && echo --Remotes && git remote -v)); }; f'
然后
git track
#2楼
if [ $# -eq 2 ]
then
echo "Setting tracking for branch " $1 " -> " $2
git branch --set-upstream $1 $2
else
echo "-- Local --"
git for-each-ref --shell --format="[ %(upstream:short) != '' ] && echo -e '\t%(refname:short) <--> %(upstream:short)'" refs/heads | sh
echo "-- Remote --"
REMOTES=$(git remote -v)
if [ "$REMOTES" != '' ]
then
echo $REMOTES
fi
fi
它仅显示已配置轨道的本地。
将它写在路径上名为git-track的脚本上,您将获得一个git track命令
https://github.com/albfan/git-showupstream上更详细的版本
#3楼
如果查看git-rev-parse的手册页,您将看到描述了以下语法:
<branchname>@{upstream},例如master@{upstream},@{u}对于branchname的后缀
@{upstream}(简短形式<branchname>@{u})是指由branchname指定的分支设置为在其上构建的分支。 缺少的branchname默认为当前的。
因此,要找到分支master的上游,您可以:
git rev-parse --abbrev-ref master@{upstream}
# => origin/master
要打印出每个分支的信息,您可以执行以下操作:
while read branch; do
upstream=$(git rev-parse --abbrev-ref $branch@{upstream} 2>/dev/null)
if [[ $? == 0 ]]; then
echo $branch tracks $upstream
else
echo $branch has no upstream configured
fi
done < <(git for-each-ref --format='%(refname:short)' refs/heads/*)
# Output:
# master tracks origin/master
# ...
这比手动解析refs和config更干净。
#4楼
对于当前的分支,这里有两个不错的选择:
% git rev-parse --abbrev-ref --symbolic-full-name @{u}
origin/mainline
要么
% git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD)
origin/mainline
这个答案也在这里 ,一个稍微不同的问题被错误地标记为重复。
#5楼
对于当前分支,您也可以说git checkout (没有任何分支)。 这是一个带有副作用的无操作,用于显示当前分支的跟踪信息(如果存在)。
$ git checkout
Your branch is up-to-date with 'origin/master'.
来源:oschina
链接:https://my.oschina.net/stackoom/blog/3149028