[英]In Git, can I “Checkout” all remote branches in my local repo?
We recreated our Gitlab server and I was pushing the branches I had checked out.我们重新创建了我们的 Gitlab 服务器,我正在推送我检查过的分支。 I then noticed that I could still checkout all of the other branches in the repo that I had never touched.
然后我注意到我仍然可以检查我从未接触过的 repo 中的所有其他分支。 However when I tried to push all branches to the new Gitlab server, it only pushed the branches that I had checked out, not all the branches listed.
但是,当我尝试将所有分支推送到新的 Gitlab 服务器时,它只推送了我签出的分支,而不是列出的所有分支。 Is there a way to checkout all of the branches or to push these branches to the server?
有没有办法签出所有分支或将这些分支推送到服务器?
If you run如果你跑
git branch -r
then you can see remote branches.然后你可以看到远程分支。 If you put all of those into a file and then put
如果你把所有这些都放到一个文件中,然后把
git checkout
as a prefix (with vim this is very easy to do) for all records and then run your file in the command-line, then it should checkout
all the branches.作为所有记录的前缀(使用 vim 这很容易做到),然后在命令行中运行您的文件,然后它应该
checkout
所有分支。
I then noticed that I could still checkout all of the other branches in the repo that I had never touched...
然后我注意到我仍然可以检查我从未接触过的 repo 中的所有其他分支......
For a proper, thorough answer, we need to note two things:为了获得正确、彻底的答案,我们需要注意两点:
git checkout
can check out a branch. git checkout
可以签出一个分支。 No surprise here (well, not yet).这里不足为奇(嗯,还没有)。 When you do this, Git checks out the branch.
执行此操作时,Git 会检出分支。 This involves checking out one specific commit, but also putting you "on" the branch, so that
git status
says, eg, on branch master
.这涉及检查一个特定的提交,但也将您“置于”分支上,以便
git status
表示,例如, on branch master
。
git checkout
can also check out things that are not branches. git checkout
也可以检出非分支的东西。 When you use git checkout v2.1
(where v2.1
is a tag name), for instance, Git checks out one specific commit, and calls that a detached HEAD .例如,当您使用
git checkout v2.1
(其中v2.1
是标签名称)时, Git 会签出一个特定的提交,并将其称为分离的 HEAD 。
When you are in this detached HEAD mode, you are not on any branch.当您处于此分离的 HEAD 模式时,您不在任何分支上。 As we'll see in a moment, this detached HEAD thing also applies to other non-branch-name items, including remote-tracking names like
origin/master
.正如我们稍后将看到的,这个分离的 HEAD 事物也适用于其他非分支名称项目,包括远程跟踪名称,如
origin/master
。
What we really need here is a good, solid definition of the word branch .我们在这里真正需要的是一个好的、可靠的词分支定义。 Unfortunately, in Git, there are multiple conflicting definitions, depending on how you like to use the word.
不幸的是,在 Git 中,有多个相互冲突的定义,具体取决于您喜欢如何使用这个词。 (For more about this, see What exactly do we mean by "branch"? )
(有关此的更多信息,请参阅“分支”到底是什么意思? )
The meaning we're using in item 1 above is a branch name , which some people call a local branch .我们在上面第 1 项中使用的含义是一个分支名称,有些人称之为本地分支。 These are names like
master
or develop
(vs origin/master
and origin/develop
).这些是
master
或develop
之类的名称(相对于origin/master
和origin/develop
)。 A branch name like these two—which are actually just shorthand for a longer form, refs/heads/master
and refs/heads/develop
here—has two important properties:像这两个分支名称(实际上只是较长形式的简写,此处为
refs/heads/master
和refs/heads/develop
)具有两个重要属性:
These branch names exist in your repository and are quite independent of any branch names that may or may not exist in any other repository.这些分支名称存在于您的存储库中,并且完全独立于任何其他存储库中可能存在或不存在的任何分支名称。
Names like origin/master
and origin/develop
are not exactly branch names, though some people like to call them branch names. origin/master
和origin/develop
等名称并不完全是分支名称,尽管有些人喜欢称它们为分支名称。 1 (This is where the rather wobbly definitions that Git and Git-users use cause issues.) They obey the first rule—they point to one particular commit—but not the second. 1 (这是 Git 和 Git 用户使用的相当不稳定的定义导致问题的地方。)他们遵守第一条规则——它们指向一个特定的提交——但不是第二条。 2 You can run
git checkout origin/master
, but as with the tag v2.1
, you wind up with a detached HEAD. 2您可以运行
git checkout origin/master
,但与标签v2.1
一样,您最终会得到一个分离的 HEAD。 You're not on any branch.你不在任何分支上。
So this could be the whole answer:所以这可能是整个答案:
git checkout branch
checks out a (local) branch that you already have, and git checkout branch
您已经拥有的(本地)分支,并且git checkout remote-tracking-name
checks out the commit identified by a remote-tracking name. git checkout remote-tracking-name
签出由远程跟踪名称标识的提交。 But it's not the whole answer, because git checkout
can create a branch for you.但这不是完整的答案,因为
git checkout
可以为您创建一个分支。 3 Suppose that you don't have a local branch named develop
, but do have a remote-tracking name origin/develop
. 3假设您没有名为
develop
的本地分支,但有一个远程跟踪名称origin/develop
。 Running:跑步:
git checkout develop
will typically succeed, and will work by first creating a (local) develop
using origin/develop
to set both its upstream and its current commit hash ID.通常会成功,并且首先使用
origin/develop
创建一个(本地) develop
来设置其上游和当前提交 hash ID。 4 That is, Git will first look for the existing branch name develop
. 4即 Git 会先寻找已有的分支名称
develop
。 If that does exist, Git will (try to 5 ) check out that branch, and be done.如果确实存在, Git 将(尝试5 )检查该分支,然后完成。 But if not , rather than immediately failing, Git will poke around to see if you have a remote-tracking name that looks similar, such as
origin/develop
.但如果没有,而不是立即失败,Git 会四处寻找,看看您是否有一个看起来相似的远程跟踪名称,例如
origin/develop
。 If so, checkout uses what Git calls DWIM —Do What I Mean—to turn this into git checkout -b develop origin/develop
.如果是这样,结帐使用 Git 所称的DWIM (按我的意思行事)将其转换为
git checkout -b develop origin/develop
。
So to the two above, we must add:因此,对于上述两个,我们必须添加:
git checkout name-that-does-not-yet-exist
will, if it can, create a branch and check it out. git checkout name-that-does-not-yet-exist
如果可以,将创建一个分支并检查它。 The "DWIM" feature finds a remote-tracking name to use as a guide and creates the branch and checks it out. 1 Note that remote-tracking names origin/master
and origin/develop
are also shortened forms, for refs/remotes/origin/master
and refs/remotes/origin/develop
respectively. 1请注意,远程跟踪名称
origin/master
和origin/develop
也缩写为 forms,分别用于refs/remotes/origin/master
和refs/remotes/origin/develop
。 If you ever accidentally create a local branch named origin/X
, its full name is refs/heads/origin/X
and that's what makes it a local branch.如果您不小心创建了一个名为
origin/X
的本地分支,它的全名是refs/heads/origin/X
,这就是使它成为本地分支的原因。 You should rename or remove it quickly, though, because Git will shorten it to origin/X
and that may well confuse you, especially if you also have a refs/remotes/origin/X
, which Git will also shorten to origin/X
.但是,您应该快速重命名或删除它,因为 Git 会将其缩短为
origin/X
,这可能会让您感到困惑,特别是如果您还有refs/remotes/origin/X
, Git也会缩短为origin/X
。
2 Git does update remote-tracking names, but it does so when you run git fetch
. 2 Git确实更新了远程跟踪名称,但是当您运行
git fetch
时它会这样做。 Your Git calls up some other Git—the one over at origin
, for instance—and asks that Git which commit each of its branch names, master
and develop
in this case, identifies.您的 Git 调用其他一些 Git - 例如,在
origin
的那个- 并要求develop
提交其每个分支名称,在这种情况下是master
和 development ,识别。 Your Git then gets those commits if needed.如果需要,您的 Git 然后会获取这些提交。 If you already have those commits, your Git gets to skip this step.
如果你已经有这些提交,你的 Git 可以跳过这一步。 Then, with the commits locally available in your repository, your Git now creates or updates your remote-tracking names corresponding to the branch names in the other Git.
然后,使用存储库中本地可用的提交,您的 Git 现在创建或更新与其他 Git 中的分支名称相对应的远程跟踪名称。
Since you can't be "on" a remote-tracking name, updating those names at git fetch
time is safe.由于您不能“启用”远程跟踪名称,因此在
git fetch
时间更新这些名称是安全的。
3 In fact, it has a lot of create-a-branch modes, using git checkout -b
, git checkout -B
, git checkout --track
, and so on. 3其实它有很多create-a-branch模式,使用
git checkout -b
, git checkout -B
, git checkout --track
,等等But here we're looking only at what Git calls "DWIM".但在这里,我们只关注 Git 所称的“DWIM”。
4 Some of this is configurable. 4其中一些是可配置的。 The description here covers only the default actions, to keep this answer from being even longer.
此处的描述仅涵盖默认操作,以防止此答案更长。
5 git checkout
will sometimes fail with an error, in which case it should have left everything alone. 5
git checkout
有时会因错误而失败,在这种情况下,它应该不理会所有内容。 The error message is meant to tell you why it did nothing.错误消息旨在告诉您为什么它什么也没做。
Besides checking out a local branch in the usual way, there are two ways that you can git checkout
"remote branches"—whatever you may mean by "remote branches"—because you can either use detached HEAD mode to check out, and thus inspect, origin/foo
, or you can use DWIM mode to create a local foo
using origin/foo
, and check that out as a local branch in the usual way.除了以通常的方式检出本地分支外,还有两种方法可以
git checkout
“远程分支”——无论你所说的“远程分支”是什么意思——因为你可以使用分离 HEAD 模式检出,从而检查, origin/foo
,或者您可以使用 DWIM 模式使用origin/foo
创建本地foo
,并以通常的方式将其作为本地分支检查。
If you do use DWIM mode, you accumulate a bunch of new local branches.如果你确实使用 DWIM 模式,你会积累一堆新的本地分支。 Once you have done that, those are your branch names.
完成后,这些就是您的分支名称。 They will not automatically update when something happens in the other Git.
当另一个Git 发生问题时,它们不会自动更新。 They will only update when you are "on" them and do something that updates them.
它们只会在您“开启”它们并执行更新它们的操作时更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.