远程 Git 分支不可见

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

Remote Git branches not visible

git

提问by Daniel

After git pullI've always been able to see all branches by doing a git branch.

git pull我总是能够通过执行git branch.

Right now (for some reason) running git branchshows me master and one other branch, that I am actively working on,.

现在(出于某种原因)运行git branch向我展示了 master 和另一个我正在积极工作的分支。

git remote show originshows me all branches. If I do a git checkouton one of them, it pulls the branch down locally and swaps to it. After that it's visible when I run git branch

git remote show origin向我展示了所有分支。如果我git checkout对其中一个执行 a ,它会在本地拉下分支并交换到它。之后,当我运行时它是可见的git branch

Question

How do I get the remote branches to display again when I run git branch?

运行时如何让远程分支再次显示git branch

Note: This is only a visual problem - I can still access my remote branches without problems, I'm just wondering why they're not showing up when I do a git branch as they usually do

注意:这只是一个视觉问题 - 我仍然可以毫无问题地访问我的远程分支,我只是想知道为什么当我执行 git 分支时它们没有像通常那样显示

回答by torek

TL;DR: just use git branch -ror git branch -a(after git fetchto update). It's not at all clear why you've been seeing remote-tracking branches without these flags (perhaps you have been using a GUI that shows them automatically?).

TL;DR:只需使用git branch -rgit branch -agit fetch更新后)。完全不清楚为什么您会看到没有这些标志的远程跟踪分支(也许您一直在使用自动显示它们的 GUI?)。

You do, however, have at least one small misconception built in to your question—which is not surprising; this particular part of Git is tricky at first.

但是,您的问题中至少存在一个小的误解——这并不奇怪;Git 的这个特殊部分起初很棘手。



There are, in fact, threesets of branch names involved in this question.

事实上,这个问题涉及组分支名称。

git remote show originshows me all branches.

git remote show origin向我展示了所有分支。

Not exactly. Let's back up a bit, and define twosets (or classes, or whatever word you like to group them) of branches. Git provides:

不完全是。让我们稍微备份一下,并定义两个分支集合(或类,或任何您喜欢将它们分组的词)。Git 提供:

  • Your regular, ordinary, local branches. These are what git branchshows, when used with no arguments and flags.

    $ git branch
      diff-merge-base
    * master
      precious
      stash-exp
    

    The word "branch" often means one of these: a namethat, when fed to git rev-parse, resolves to a commit ID:

    $ git rev-parse diff-merge-base
    2d0cc5001c1a88995727521d4ef77f7a4acc4e14
    

    and whose full name starts with refs/heads/:

    $ git rev-parse --symbolic-full-name diff-merge-base
    refs/heads/diff-merge-base
    
  • Your remote-tracking branches. These are what git branch -rshows:

    $ git branch -r
      origin/HEAD -> origin/master
      origin/maint
      origin/master
      origin/next
      origin/pu
      origin/todo
    
  • 您的常规、普通、本地分支机构。这些是git branch在不带参数和标志的情况下使用时显示的内容。

    $ git branch
      diff-merge-base
    * master
      precious
      stash-exp
    

    “分支”这个词通常意味着其中之一:一个名称,当被馈送到 时git rev-parse,解析为提交 ID:

    $ git rev-parse diff-merge-base
    2d0cc5001c1a88995727521d4ef77f7a4acc4e14
    

    其全名以refs/heads/

    $ git rev-parse --symbolic-full-name diff-merge-base
    refs/heads/diff-merge-base
    
  • 您的远程跟踪分支。这些是git branch -r显示:

    $ git branch -r
      origin/HEAD -> origin/master
      origin/maint
      origin/master
      origin/next
      origin/pu
      origin/todo
    

The key difference between these is that your local branches are your names to manipulate however you like, while your remote-tracking branches are your names that Git automatically slaves to something else. It is possiblefor you to manipulate them yourself, but it's not profitable, as their intent is to remember some otherGit's branch names (and corresponding SHA-1 values).

它们之间的主要区别在于,您的本地分支是您可以随意操作的名称,而您的远程跟踪分支是您的名称,Git 会自动从属于其他东西。这是可能的,你自己对它们操作,但它不是赚钱的,因为他们的意图是要记住一些其他Git的分支名称(以及相应的SHA-1的值)。

Note that feeding a remote-tracking branch name to git rev-parsealsoworks, and you can get its symbolic-full-nameas well: this simply begins with refs/remotes/, followed by the name of the remote, followed by the localname as seen if you're the Git running onthe remote. Hence:

注意喂养远程跟踪分行名称git rev-parse工作正常,你可以得到它symbolic-full-name,以及:这只是开头refs/remotes/,后跟远程,其次是名称的本地名称所看到好像你的Git的运行的远程。因此:

$ git rev-parse --symbolic-full-name origin/master
refs/remotes/origin/master

means that my Git's origin/masteris my Git's memoryof what mastermeant, on origin, the last time had my Git call up originand fetch—i.e., update—from them.

意味着我的Git的origin/master是我的Git的记忆是什么master意思,上origin,上一次有我的Git调用origin和获取,即更新从他们。

git remote(sometimes) actually calls up the remote Git

git remote(有时)实际上调用了远程 Git

Remember that there are two(or sometimes even more) Git version-control databases involved any time you fetch or push commits. So you can look at yourinformation, or you can ask your Git to call up theirGit, over the Internet-phone, and query themabout theirinformation. They, too, may have local branches, and even remote-tracking branches, of their own. (Usually, for situations like this, they have only local branches.)

请记住,每次获取或推送提交时,都会涉及两个(有时甚至更多)Git 版本控制数据库。所以,你可以看到你的信息,或者您可以询问您的Git来调用它们的Git,通过互联网,电话和查询他们关于他们的信息。他们也可能拥有自己的本地分支机构,甚至远程跟踪分支机构。(通常,对于这种情况,他们只有本地分支机构。)

For illustration purposes, let me delete one of my own remote-tracking branches (this is pretty harmless since I will run git fetchin a moment to restore it):

出于说明目的,让我删除我自己的一个远程跟踪分支(这是非常无害的,因为我将git fetch在稍后运行以恢复它):

$ git branch -r -d origin/pu
Deleted remote-tracking branch origin/pu (was 7c79844).

Now if I run git branch -rI will no longer have an origin/pu: my Git no longer has that as a remote-tracking branch. But theirGit, over on origin, still has a localbranch named pu, so:

现在,如果我运行,git branch -r我将不再拥有origin/pu: 我的 Git 不再将其作为远程跟踪分支。但是他们的Gitorigin仍然有一个名为的本地分支pu,所以:

$ git remote show origin
* remote origin
  Fetch URL: git://git.kernel.org/pub/scm/git/git.git
  Push  URL: git://git.kernel.org/pub/scm/git/git.git
  HEAD branch: master
  Remote branches:
    maint  tracked
    master tracked
    next   tracked
    pu     new (next fetch will store in remotes/origin)
    todo   tracked
  Local branches configured for 'git pull':
    master    merges with remote master
    stash-exp merges with remote master
  Local ref configured for 'git push':
    master pushes to master (local out of date)

When I run git remote show origin, my Git calls up their Git (this happens to be a copy of the Git repository for Git—there's another one on github.com that is probably more appropriate these days) and gets from them a list of all their branches. I already have most of them as my own "remote-tracking branches", but I deleted pu, so it shows as "new".

当我运行 时git remote show origin,我的 Git 调用他们的 Git(这恰好是 Git 的 Git 存储库的副本——现在 github.com 上还有另一个可能更合适)并从他们那里获取他们所有分支的列表。我已经将其中大部分作为我自己的“远程跟踪分支”,但我删除了pu,因此它显示为“新”。

A similar command, git ls-remote, also calls up the other Git and queries it, but shows you more: it shows you the commit hash that goes with each branch (and the object hash for each tag as well). There are a lot of tags, so let me restrict this to just one branch:

一个类似的命令,git ls-remote也调用另一个 Git 并查询它,但会显示更多信息:它会显示每个分支的提交哈希(以及每个标签的对象哈希)。有很多标签,所以让我将其限制为一个分支:

$ git ls-remote origin master
e05806da9ec4aff8adfed142ab2a2b3b02e33c8c        refs/heads/master

In both these cases, your Git (or my Git) calls up their Git and gets information from them, but it merely displays it, rather than savingit. To savethe information, we must run git fetch. I have not run this in a little while, so:

在这两种情况下,你的 Git(或我的 Git)调用他们的 Git 并从他们那里获取信息,但它只是显示它,而不是保存它。要保存信息,我们必须运行git fetch. 我有一段时间没有运行它,所以:

$ git fetch origin
remote: Counting objects: 2064, done.
remote: Compressing objects: 100% (1294/1294), done.
remote: Total 2064 (delta 1383), reused 1118 (delta 767)
Receiving objects: 100% (2064/2064), 2.12 MiB | 2.29 MiB/s, done.
Resolving deltas: 100% (1383/1383), done.
From git://git.kernel.org/pub/scm/git/git
   de2efeb..e05806d  master     -> origin/master
   3074f94..c69c2f5  next       -> origin/next
 * [new branch]      pu         -> origin/pu
   1a46792..2135c1c  todo       -> origin/todo

This git fetchdid the same thing as git remote showand git ls-remote, but then also did more things: it collected the objectsI need to complete myrepository, then updated myremote-tracking branch names to correspond to the branch names in theirrepository.

git fetchgit remote showand做了同样的事情git ls-remote,但也做了更多的事情:它收集了我需要完成我的存储库的对象,然后更新我的远程跟踪分支名称以对应于他们存储库中的分支名称。

This is why I regained origin/pu: they still have a pu, and I discarded mine, so I obtained theirs and now have one again.

这就是我重新获得的原因origin/pu:他们还有一个pu,我丢弃了我的,所以我得到了他们的,现在又拥有了。

This is also why I updated all the others except maint: it's been long enough that they have all been updated. My origin/masterused to refer to commit de2efeb, but now it refers to e05806d, which is the same ID we saw above when we ran git ls-remote. (This means they did not update their masterin the several minutes it took me to type all this in. Imagine that, billionsof nanoseconds have gone by without an update! :-) )

这也是我更新所有其他内容的原因,除了maint:它们已经更新了足够长的时间。Myorigin/master过去指的是 commit de2efeb,但现在指的是e05806d,这与我们在上面运行时看到的 ID 相同git ls-remote。(这意味着他们master在我输入所有这些内容的几分钟内没有更新他们的内容。想象一下,数十亿纳秒没有更新!:-))

(Note that git remote show -n originskipsthe phoning-up of origin, and simply shows you what your Git has recorded. Several other git remotecommands also work locally; consult the documentation for specifics.)

(请注意,这里git remote show -n origin跳过了 的调用origin,只是向您展示了您的 Git 记录的内容。其他几个git remote命令也可以在本地运行;请参阅文档了解详细信息。)

Summary

概括

To recap, then, there are threesets of branch names involved here:

回顾一下,这里涉及组分支名称:

  • Your local branches;
  • Your remote-tracking branches; and
  • Their branches (in the remote Git).
  • 您当地的分支机构;
  • 您的远程跟踪分支;和
  • 他们的分支(在远程 Git 中)。

The last set of branch names don't matter, most of the time. Most of the time, you work in your own repository. There's only one Git involved, and it's yours. TheirGit, which has its own branches, is right out of the picture.

大多数情况下,最后一组分支名称无关紧要。大多数时候,您在自己的存储库中工作。只涉及一个 Git,它是你的。 他们的Git 有自己的分支,就在图片之外。

But sometimes—ah, such times!—sometimes, you must connect your Git to anotherGit. Nowtheir names matter! The really tricky thing here is that your names don't haveto match their names at all. There's no hard and fast reason yourmastermustcorrespond to their master—but yourGit will copy theirmasterto yourorigin/master, so it saves a lot of brain cells if your names and their names domatch up. You canwork around this (in multiple different ways), but don't do it until you have a situation that really calls for it (with multiple remotes that use conflicting branch names—this almost never happens).

但有时——啊,这样的时候!——有时,你必须将你的 Git 连接到另一个Git。 现在他们的名字很重要!这里的真正棘手的事情是,你的名字不具有匹配他们的名字在所有。有没有硬性的原因,master必须符合它们master-但你的Git会复制他们master你的origin/master,所以它节省了大量的脑细胞,如果你的名字和自己的名字匹配。你可以解决这个问题(以多种不同的方式),但在遇到真正需要它的情况之前不要这样做(多个遥控器使用冲突的分支名称——这几乎从未发生过)。

What about git checkout blah?

怎么样git checkout blah

You noted above that:

您在上面指出:

If I do a git checkouton one of [their branch names that I don't see in git branchoutput], it pulls the branch down locally and swaps to it.

如果我git checkout对 [他们在git branch输出中看不到的分支名称] 之一执行操作,它会在本地拉下分支并交换到它。

Let's say that you have run git branch -r(rather than git remote show origin) and seen a branch named origin/zorg. Let's also say you don't already have a (local) branch named zorg. You run:

假设您已经运行git branch -r(而不是git remote show origin)并看到了一个名为 的分支origin/zorg。假设您还没有名为zorg. 你跑:

$ git checkout zorg

and your Git says:

你的 Git 说:

Branch zorg set up to track remote branch zorg from origin.
Switched to a new branch 'zorg'

Your Git hasn't "pulled down" anythinghere. What it's done is to create a new local branch name, zorg, pointing to the same commit—the same big ugly SHA-1 hash ID—as origin/zorg. That commit was already in your repository, ready to be checked out any time, and in fact you could have done:

你的 Git 没有在这里“拉下”任何东西。那是什么做的是创建一个新的本地分支的名字,zorg指着同-the同大丑SHA-1散列ID-作为origin/zorg。该提交已经在您的存储库中,随时可以检出,实际上您可以这样做:

$ git checkout origin/zorg

to look at it—but that would give you what Git calls a "detached HEAD".

看看它——但这会给你一个 Git 所谓的“分离的 HEAD”。

What's going on here is that in Git, a branch nameis merely a moveable pointer to one specific commit. The git checkoutcommand, when used this way, does twothings: check out the one specific commit (into the work-tree), and, switch your Git's notion of the "current branch name". When you git checkoutan existing, local, ordinary branch name, Git checks out the one commit in question, and puts you "on the branch", as git statuswill say:

这里发生的事情是,在 Git 中,分支名称只是一个指向特定提交的可移动指针。以git checkout这种方式使用时,该命令会做件事:检查一个特定的提交(进入工作树),以及切换 Git 的“当前分支名称”概念。当您git checkout使用现有的、本地的、普通的分支名称时,Git 会检查有问题的一个提交,并将您置于“分支上”,如下所示git status

$ git status
On branch master

When you git checkoutany commit by something that's nota simple branch name, Git still checks out the one commit, but takes you off of any branch, i.e., gives you a "detached HEAD".

当您git checkout通过不是简单分支名称的任何内容提交时,Git 仍会检查一次提交,但会将您从任何分支中移除,即,给您一个“分离的 HEAD”。

We know from the above that origin/masteris (now) commit e05806da9ec4aff8adfed142ab2a2b3b02e33c8c, so:

我们从上面知道origin/master是(现在) commit e05806da9ec4aff8adfed142ab2a2b3b02e33c8c,所以:

$ git checkout origin/master

or:

或者:

$ git checkout e05806da9ec4aff8adfed142ab2a2b3b02e33c8c

both do the same thing. Since origin/masteris nota localbranch, I end up with:

两者都做同样的事情。既然origin/master不是一个地方分支,我结束了:

$ git status
HEAD detached at e05806d

(or sometimes HEAD detached at origin/master).

(或有时HEAD detached at origin/master)。

Hence, what git checkoutdoes is attempt to convert the name you gave it to a branch name. If that fails, git checkouthas this extra built-in feature: it searches through all your remote-tracking branchesto see if there's exactly one that "mostly matches" the name. So git checkout zorgchecks for a local branch named zorg, fails to find it, then searches all your remote-tracking branches for one that alsomatches zorg. There is in fact exactly one—origin/zorg—so this triggers the special case code.

因此,什么git checkout是尝试将您给它的名称转换为分支名称。 如果失败,则git checkout具有这个额外的内置功能:它会搜索所有远程跟踪分支,以查看是否确实有一个“主要匹配”名称的分支。因此,git checkout zorg检查名为 的本地分支zorg,但找不到它,然后在所有远程跟踪分支中搜索匹配的分支zorg。实际上正好有一个origin/zorg————所以这会触发特殊情况代码。

The special case code simply implements "create new local branch set up to track corresponding remote-tracking branch". That is, create a local zorg, with its upstream(as Git now calls these things) set to origin/zorg.

特殊情况代码简单地实现了“创建新的本地分支设置以跟踪相应的远程跟踪分支”。也就是说,创建一个 local zorg,并将其上游(Git 现在称之为这些东西)设置为origin/zorg.

Note that for this to work, there must be exactly onesuitable remote-tracking branch. If I did not have origin/zorgat all, this would fail—and if I had both origin/zorgand thirdrepo/zorg, where thirdrepois another remote, like originbut pointing to some third Git repository, it would also fail, because Git would not know whether my local zorgshould have origin/zorgor thirdrepo/zorgas its upstream.

请注意,要使其正常工作,必须恰好有一个合适的远程跟踪分支。如果我根本没有origin/zorg,这将失败——如果我同时拥有origin/zorgand thirdrepo/zorgthirdrepo另一个远程在哪里,比如origin但指向某个第三个 Git 存储库,它也会失败,因为 Git 不知道我的本地是否zorg应该拥有origin/zorgthirdrepo/zorg作为它的上游。

Most of the time, you have only one remote, named origin. So, as long as you have all of origin's branches saved as your own Git's remote-tracking branch memories, you can just git checkoutthose names to get your Git to create them. But sometimes you will find that you must run git fetchfirst, so as to update your remote-tracking branches.

大多数情况下,您只有一个遥控器,名为origin. 因此,只要您将 的所有origin分支都保存为您自己的 Git 的远程跟踪分支内存,您就可以仅使用git checkout这些名称来让您的 Git 创建它们。但有时你会发现你必须先运行git fetch,以便更新你的远程跟踪分支。

回答by Boaz Nahum

Well, I had the exact same problem, then I find that in project-specific git config (located at ./.git/config)

好吧,我遇到了完全相同的问题,然后我在特定于项目的 git config(位于 ./.git/config)中发现了这个问题

remote.origin.fetch

remote.origin.fetch

was something other than

是别的东西

fetch = +refs/heads/*:refs/remotes/origin/*

Something like:

就像是:

[remote "origin"]
  url = https://....
  fetch = +refs/heads/v8.1:refs/remotes/origin/v8.1

After changing it to:

改成后:

[remote "origin"]
  url = https://....
  fetch = +refs/heads/*:refs/remotes/origin/*

now 'git branch -avv' show all remote tracking branches

现在 'git branch -avv' 显示所有远程跟踪分支

If you like to know from where I got this weird settings, it is because i used 'clone --single-branch'

如果你想知道我从哪里得到这个奇怪的设置,那是因为我使用了 'clone --single-branch'

回答by VelikiiNehochuha

1) just show branches:

1)只显示分支:

git branch -r

2) load remote branches into local git:

2) 将远程分支加载到本地 git 中:

git fetch origin/<remote_name>

3) create local branch from remote branch (after call it you can view branch into out of git branch):

3)从远程分支创建本地分支(调用它后您可以查看分支到git分支之外):

git checkout -b <remote_name> origin/<remote_name>

回答by Arpit Aggarwal

Can you try get-fetchas:

您可以尝试get-fetch为:

git fetch <remote>

回答by Pierre de Buyl

git branch -a

will list all the branches, the remotes one will appear in red prefixed by the name of the remote.

将列出所有分支,遥控器将显示为红色,并带有遥控器名称的前缀。

回答by workabyte

for me I was pulling/fetching from originbut needed to fetch from upstream

对我来说,我正在拉/取,origin但需要从上游取

get fetch upstreamand then i was able to checkout the missing branch

get fetch upstream然后我就可以查看丢失的分支

回答by Rohit Chaurasiya

git branch

git 分支

o/p : master xyz

o/p : 主 xyz

git branch -u origin/[branch Name]

git branch -u origin/[分支名称]

ex : git branch -u origin/developer

例如:git branch -u origin/developer

Then checkout : git checkout [branch Name]

然后结帐: git checkout [分支名称]

ex : git checkout sonarfix-fvfr-parts-splitter

例如:git checkout sonarfix-fvfr-parts-splitter

git branch

git 分支

o/p : master xyz developer

o/p : xyz 开发大师

回答by Sarvesh Pandey

Accepted answer is the perfect one and solved my purpose. I'm just sharing what worked for me. Let's start with problem, I was trying to access a branch named 'NewBranch' that I have already created but I got an error, something like this:

接受的答案是完美的答案并解决了我的目的。我只是分享对我有用的东西。让我们从问题开始,我试图访问我已经创建的名为“NewBranch”的分支,但出现错误,如下所示:

error: pathspec 'NewBranch' did not match any file(s) known to git

So, I tried this git command,

所以,我尝试了这个 git 命令,

git remote show origin

The above command will give you the list of all branches. And, I found that the branch I am trying to checkout is new and not fetched yet. Something like this,

上面的命令会给你所有分支的列表。而且,我发现我尝试结帐的分支是新的,尚未提取。像这样的东西,

  Remote branches:
DH5982                               new (next fetch will store in remotes/origin)
ML                                   tracked
Sarvesh_Branch                       tracked
NewBranch                   new (next fetch will store in remotes/origin)
auc_branch                           tracked
fileSize                             tracked
fileuploadtime                       tracked
master                               tracked
refs/remotes/origin/DH-5697          stale (use 'git remote prune' to remove)
refs/remotes/origin/DH-5914          stale (use 'git remote prune' to remove)

So, I simply used this git command,

所以,我只是使用了这个 git 命令,

git fetch --all

And, then its done, and 'NewBranch' became accessible to me.

然后它就完成了,我可以访问“NewBranch”。