【问题标题】:Find out which remote a local branch is tracking找出本地分支正在跟踪哪个远程
【发布时间】:2012-09-18 12:47:04
【问题描述】:

不是Find out which remote branch a local branch is tracking,如果我有多个遥控器,我可能在所有遥控器中都有“主人”。 git branch 返回 master 但我不知道我所在的 master 分支是在 remoteFoo 还是 remoteBar。例如,我可能会这样做:

git clone someRepo.git
cd someRepo
git remote add anotherRemote otherremoteURL

然后git remote 显示

someRepo
anotherRemote

我可以做到git checkout -b master someRepo/mastergit checkout -b master anotherRemote/mastergit branch 在这两种情况下都会说“主人”。如何取回第一部分“someRepo”或“anotherRemote”?

您可能认为我可以使用 git remote show,但它需要一个参数,即您想要获取信息的遥控器的名称。

$ git remote show origin
fatal: 'origin' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
$ git remote show
someRepo
anotherRemote

通过git branch,我可以了解当前的内容:

$ git branch
  hold
* master
  old-stuff
  refactor

git remote 输出中没有“*”。

【问题讨论】:

  • 它与引用的问题究竟有何不同? This answer 似乎提供了有关本地分支机构跟踪的远程分支机构的详细信息;如果你这样做git checkout someRepo/master,git 会明确告诉你你处于分离的 HEAD 状态,git branch 会说(no branch),而不是你想象的master
  • 也许我错过了,但在那个问题中我没有看到任何命令显示当前分支的远程,只有分支名称。
  • git branch 为您提供当前分支的名称,git remote show origin 告诉您哪个本地分支跟踪哪个远程。可能不会有一个特定的命令以您想要的特定方式处理您的特定情况。

标签: git


【解决方案1】:

您可能想尝试以下这些以查看详细信息。

git remote -v
git remote -v show origin

下面的示例输出:

dmasi@:/var/www/pirate_booty$ git remote -v
origin  git@bitbucket.org:dmasi/pirate_booty_calculator.git (fetch)
origin  git@bitbucket.org:dmasi/pirate_booty_calculator.git (push)

dmasi@:/var/www/pirate_booty$ git remote -v show origin
* remote origin
  Fetch URL: git@bitbucket.org:dmasi/pirate_booty_calculator.git
  Push  URL: git@bitbucket.org:dmasi/pirate_booty_calculator.git
  HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

【讨论】:

  • 谢谢! git remote 列出了我所有的遥控器,git remote -v 显示了当前遥控器的状态,因此我可以将该输出的第一行的第一个单词用作我当前的遥控器。
【解决方案2】:

git status -sb 似乎很容易理解和记住。

-s 代表以短格式给出输出。

b 用于显示分支和跟踪信息。

参考:https://git-scm.com/docs/git-status#git-status--s

【讨论】:

    【解决方案3】:

    这在一个类似的问题here中得到了回答:要让当前分支跟踪远程分支,

    git rev-parse --symbolic-full-name --abbrev-ref @{u}
    

    【讨论】:

    • 这给了我错误(错误:没有为分支'master'配置上游)。您使用的是哪个版本的git?我在 1.7.11.3。
    • 1.7.12。如果您没有上游分支,那么可能还有另一个问题。这是一个新的分支,你把它推到远程了吗? git branch -a 的输出是什么?
    猜你喜欢
    • 2010-09-15
    • 2013-04-30
    • 2012-10-14
    • 2021-03-21
    • 1970-01-01
    • 2011-07-27
    • 2011-06-24
    • 2014-07-31
    相关资源
    最近更新 更多