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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4950725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:04:30  来源:igfitidea点击:

How can I see which Git branches are tracking which remote / upstream branch?

git

提问by joachim

I know I can do git branch --all, and that shows me both local and remote branches, but it's not that useful in showing me the relationships between them.

我知道我可以做到git branch --all,这向我展示了本地和远程分支,但在向我展示它们之间的关系时并没有那么有用。

How do I list branches in a way that shows which local branch is tracking which remote?

如何以显示哪个本地分支跟踪哪个远程的方式列出分支?

回答by Cascabel

Very much a porcelain command, not good if you want this for scripting:

非常像瓷器命令,如果你想用它来编写脚本就不好:

git branch -vv   # doubly verbose!

Note that with git 1.8.3, that upstream branch is displayed in blue(see "What is this branch tracking (if anything) in git?")

请注意,在 git 1.8.3 中,上游分支显示为蓝色(请参阅“ git 中的此分支跟踪(如果有的话)是什么?”)



If you want clean output, see arcresu's answer- it uses a porcelain command that I don't believe existed at the time I originally wrote this answer, so it's a bit more concise and works with branches configured for rebase, not just merge.

如果您想要干净的输出,请参阅arcresu 的答案- 它使用了一个瓷器命令,我认为在我最初编写此答案时不存在该命令,因此它更简洁一些并且适用于为 rebase 配置的分支,而不仅仅是合并。

回答by kubi

git remote show origin

git remote show origin

Replace 'origin' with whatever the name of your remote is.

用遥控器的任何名称替换“origin”。

回答by Carl Suster

If you look at the man page for git-rev-parse, you'll see the following syntax is described:

如果您查看 的手册页git-rev-parse,您会看到描述了以下语法:

<branchname>@{upstream}, e.g. master@{upstream}, @{u}

The suffix @{upstream}to a branchname (short form <branchname>@{u}) refers to the branch that the branch specified by branchname is set to build on top of. A missing branchname defaults to the current one.

<branchname>@{upstream},例如master@{upstream}@{u}

分支名称的后缀@{upstream}(简称 <branchname>@{u})指的是由 branchname 指定的分支设置为在其上构建的分支。缺少的分支名称默认为当前的分支名称。

Hence to find the upstream of the branch master, you would do:

因此要找到 branch 的上游master,你会这样做:

git rev-parse --abbrev-ref master@{upstream}
# => origin/master

To print out the information for each branch, you could do something like:

要打印出每个分支的信息,您可以执行以下操作:

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
# ...

This is cleaner than parsing refs and config manually.

这比手动解析 refs 和 config 更干净。

回答by Abizern

An alternative to kubi's answer is to have a look at the .git/configfile which shows the local repository configuration:

kubi 答案的替代方法是查看.git/config显示本地存储库配置的文件:

cat .git/config

cat .git/config

回答by Aurelien

git for-each-ref --format='%(refname:short) <- %(upstream:short)' refs/heads

will show a line for each local branch. A tracking branch will look like:

将为每个本地分支显示一行。跟踪分支将如下所示:

master <- origin/master

A non-tracking one will look like:

一个非跟踪的看起来像:

test <- 

回答by cdunn2001

For the currentbranch, here are two good choices:

对于当前分支,这里有两个不错的选择:

% git rev-parse --abbrev-ref --symbolic-full-name @{u}
origin/mainline

or

或者

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

That answer is also here, to a slightly different question which was (wrongly) marked as a duplicate.

这个答案也在这里,对于一个稍微不同的问题,该问题(错误地)标记为重复。

回答by Eugene Yarmash

For the current branch, you could also say git checkout(w/o any branch). This is a no-op with a side-effects to show the tracking information, if exists, for the current branch.

对于当前分支,您也可以说git checkout(w/o any branch)。这是一个带有副作用的空操作,用于显示当前分支的跟踪信息(如果存在)。

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

回答by Olivier Refalo

I use this alias

我用这个别名

git config --global alias.track '!f() { ([ $# -eq 2 ] && ( echo "Setting tracking for branch "  " -> " ;git branch --set-upstream  ; ) || ( git for-each-ref --format="local: %(refname:short) <--sync--> remote: %(upstream:short)" refs/heads && echo --Remotes && git remote -v)); }; f'

then

然后

git track

回答by albfan

Based on Olivier Refalo's answer

基于Olivier Refalo 的回答

if [ $# -eq 2 ] 
then
    echo "Setting tracking for branch "  " -> " 
    git branch --set-upstream  
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

It shows only local with track configured.

它仅显示配置了轨道的本地。

Write it on a script called git-trackon your path an you will get a git trackcommand

把它写在你路径上一个叫做git-track的脚本上,你会得到一个git track命令

A more elaborated version on https://github.com/albfan/git-showupstream

https://github.com/albfan/git-showupstream上更详细的版本

回答by masukomi

git config --get-regexp "branch\.$current_branch\.remote"

git config --get-regexp "branch\.$current_branch\.remote"

will give you the name of the remote that is being tracked

将为您提供正在跟踪的遥控器的名称

git config --get-regexp "branch\.$current_branch\.merge"

git config --get-regexp "branch\.$current_branch\.merge"

will give you the name of the remote branch that's being tracked.

将为您提供正在跟踪的远程分支的名称。

You'll need to replace $current_branch with the name of your current branch. You can get that dynamically with git rev-parse --abbrev-ref HEAD

您需要将 $current_branch 替换为当前分支的名称。你可以用git rev-parse --abbrev-ref HEAD

The following mini-script combines those things. Stick it in a file named git-tracking, make it executable, and make sure it's in your path.

下面的小脚本结合了这些东西。将其粘贴在名为 的文件中git-tracking,使其可执行,并确保它在您的路径中。

then you can say

那么你可以说

$ git  tracking
<current_branch_name>-><remote_repo_name>/<remote_branch_name>

note that the remote branch name can be different from your local branch name (although it usually isn't). For example:

请注意,远程分支名称可以与您的本地分支名称不同(尽管通常不是)。例如:

$git tracking 
xxx_xls_xslx_thing -> origin/totally_bogus

as you can see in the code the key to this is extracting the data from the git config. I just use sed to clear out the extraneous data.

正如您在代码中看到的,关键是从 git 配置中提取数据。我只是使用 sed 清除无关数据。

#!/bin/sh

current_branch=$(git rev-parse --abbrev-ref HEAD)
remote=$(git config --get-regexp "branch\.$current_branch\.remote" | sed -e "s/^.* //")
remote_branch=$(git config --get-regexp "branch\.$current_branch\.merge" | \
  sed -e "s/^.* //" -e "s/refs\/.*\///")

echo "$current_branch -> $remote/$remote_branch"