git 找出本地分支正在跟踪哪个远程分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/171550/
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 05:51:07  来源:igfitidea点击:

Find out which remote branch a local branch is tracking

gitbranchgit-remote

提问by Readonly

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

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

How can I find out which remote branch a local branch is tracking?

如何找出本地分支正在跟踪哪个远程分支?

Do I need to parse git configoutput, or is there a command that would do this for me?

我是否需要解析git config输出,或者是否有可以为我执行此操作的命令?

回答by jdsumsion

Hereis a command that gives you all tracking branches (configured for 'pull'), see:

是一个命令,可为您提供所有跟踪分支(配置为“拉”),请参阅:

$ git branch -vv
  main   aaf02f0 [main/master: ahead 25] Some other commit
* master add0a03 [jdsumsion/master] Some commit

You have to wade through the SHA and any long-wrapping commit messages, but it's quick to type and I get the tracking branches aligned vertically in the 3rd column.

您必须仔细阅读 SHA 和任何长包装提交消息,但输入速度很快,我在第 3 列中将跟踪分支垂直对齐。

If you need info on both 'pull' and 'push' configuration per branch, see the other answer on git remote show origin.

如果您需要有关每个分支的“拉”和“推”配置的信息,请参阅 上的其他答案git remote show origin



Update

更新

Starting in git version 1.8.5 you can show the upstream branch with git statusand git status -sb

从 git 版本 1.8.5 开始,您可以使用git status和显示上游分支git status -sb

回答by cdunn2001

Two 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

回答by Ajit George

I think git branch -avonly tells you what branches you have and which commit they're at, leaving you to infer which remote branches the local branches are tracking.

我认为git branch -av只告诉你你有哪些分支以及它们在哪个提交,让你推断本地分支正在跟踪哪些远程分支。

git remote show originexplicitly tells you which branches are tracking which remote branches. Here's example output from a repository with a single commit and a remote branch called abranch:

git remote show origin明确告诉您哪些分支正在跟踪哪些远程分支。以下是来自一个存储库的示例输出,其中包含一个提交和一个名为 的远程分支abranch

$ git branch -av
* abranch                d875bf4 initial commit
  master                 d875bf4 initial commit
  remotes/origin/HEAD    -> origin/master
  remotes/origin/abranch d875bf4 initial commit
  remotes/origin/master  d875bf4 initial commit

versus

相对

$ git remote show origin
* remote origin
  Fetch URL: /home/ageorge/tmp/d/../exrepo/
  Push  URL: /home/ageorge/tmp/d/../exrepo/
  HEAD branch (remote HEAD is ambiguous, may be one of the following):
    abranch
    master
  Remote branches:
    abranch tracked
    master  tracked
  Local branches configured for 'git pull':
    abranch merges with remote abranch
    master  merges with remote master
  Local refs configured for 'git push':
    abranch pushes to abranch (up to date)
    master  pushes to master  (up to date)

回答by Aaron Wells

Update:Well, it's been several years since I posted this! For my specific purpose of comparing HEAD to upstream, I now use @{u}, which is a shortcut that refers to the HEAD of the upstream tracking branch. (See https://git-scm.com/docs/gitrevisions#gitrevisions-emltbranchnamegtupstreamemegemmasterupstreamememuem).

更新:嗯,自从我发布这个已经好几年了!出于将 HEAD 与上游进行比较的特定目的,我现在使用@{u},这是指上游跟踪分支的 HEAD 的快捷方式。(参见https://git-scm.com/docs/gitrevisions#gitrevisions-emltbranchnamegtupstreamemegemmasterupstreamememuem)。

Original answer:I've run across this problem as well. I often use multiple remotes in a single repository, and it's easy to forget which one your current branch is tracking against. And sometimes it's handy to know that, such as when you want to look at your local commits via git log remotename/branchname..HEAD.

原始答案:我也遇到过这个问题。我经常在一个存储库中使用多个遥控器,很容易忘记您当前的分支正在跟踪哪个。有时知道这一点很方便,例如当您想通过git log remotename/branchname..HEAD.

All this stuff is stored in git config variables, but you don't have to parse the git config output. If you invoke git config followed by the name of a variable, it will just print the value of that variable, no parsing required. With that in mind, here are some commands to get info about your current branch's tracking setup:

所有这些东西都存储在 git config 变量中,但您不必解析 git config 输出。如果你调用 git config 后跟一个变量的名称,它只会打印该变量的值,不需要解析。考虑到这一点,这里有一些命令可以获取有关当前分支跟踪设置的信息:

LOCAL_BRANCH=`git name-rev --name-only HEAD`
TRACKING_BRANCH=`git config branch.$LOCAL_BRANCH.merge`
TRACKING_REMOTE=`git config branch.$LOCAL_BRANCH.remote`
REMOTE_URL=`git config remote.$TRACKING_REMOTE.url`

In my case, since I'm only interested in finding out the name of my current remote, I do this:

就我而言,因为我只对找出当前遥控器的名称感兴趣,所以我这样做:

git config branch.`git name-rev --name-only HEAD`.remote

回答by nikkypx

The local branches and their remotes.

本地分支机构及其遥控器。

git branch -vv 

All branches and tracking remotes.

所有分支机构和跟踪遥控器。

git branch -a -vv

See where the local branches are explicitly configured for push and pull.

查看本地分支在何处明确配置为推送和拉取。

git remote show {remote_name}

回答by rubo77

This will show you the branch you are on:

这将显示您所在的分支:

$ git branch -vv

This will show onlythe current branch you are on:

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

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

for example:

例如:

myremote/mybranch

You can find out the URL of the remotethat is used by the current branchyou are on with:

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

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

for example:

例如:

https://github.com/someone/somerepo.git

回答by Eugene Yarmash

You can use git checkout, i.e. "check out the current branch". This is a no-op with a side-effects to show the tracking information, if exists, for the current branch.

您可以使用git checkout,即“检查当前分支”。这是一个带有副作用的空操作,用于显示当前分支的跟踪信息(如果存在)。

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

回答by William Pursell

I don't know if this counts as parsing the output of git config, but this will determine the URL of the remote that master is tracking:

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

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

回答by FragLegs

Yet another way

又一种方式

git status -b --porcelain

This will give you

这会给你

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

回答by Trickmaster

Another simple way is to use

另一种简单的方法是使用

cat .git/configin a git repo

cat .git/config在 git 仓库中

This will list details for local branches

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