简体   繁体   English

在 Git 中,我可以“签出”本地仓库中的所有远程分支吗?

[英]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:为了获得正确、彻底的答案,我们需要注意两点:

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

  2. 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 ).这些是masterdevelop之类的名称(相对于origin/masterorigin/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/masterrefs/heads/develop )具有两个重要属性:

  • it contains the hash ID of one commit, and它包含一个提交的 hash ID,并且
  • Git will automatically update the hash ID stored in that name under various conditions. Git 将在各种条件下自动更新存储在该名称中的 hash ID。 In particular, when you make a new commit while "on" one of your branches, Git will store the new commit's hash ID into the branch name.特别是,当在“打开”您的一个分支时进行新提交时,Git 会将提交的 hash ID 存储到分支名称中。

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/masterorigin/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:所以这可能是整个答案:

  1. git checkout branch checks out a (local) branch that you already have, and git checkout branch您已经拥有的(本地)分支,并且
  2. 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:因此,对于上述两个,我们必须添加:

  1. 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. “DWIM”功能会找到一个远程跟踪名称以用作指导,并创建分支并将其签出。

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/masterorigin/develop也缩写为 forms,分别用于refs/remotes/origin/masterrefs/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.错误消息旨在告诉您为什么它什么也没做。


Conclusion结论

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.

相关问题 如何将本地git仓库中的所有远程分支转换为本地跟踪分支 - How can I convert all the remote branches in a local git repo into local tracking branches 在 Git 存储库中,如何有效删除已删除远程分支的本地跟踪分支? - In a Git repo how can I efficiently remove local tracking branches for deleted remote branches? 我们是否应该将所有本地git分支推送到远程仓库上? - Should we push all local git branches on the remote repo? 将本地 Git 回购推送到新的远程,包括所有分支和标签 - Push local Git repo to new remote including all branches and tags 我可以将文件添加到本地Git存储库中,但不能将其推送到远程 - Can I add a file to my local Git repo but not push it to remote git:所有本地分支都在遥控器后面。 如何将它们全部带到远程状态? - git: all local branches are behind the remote. How can I bring them all up to the remote state? 我可以将本地分支映射到 git 中具有不同前缀的远程分支吗? - Can I map local branches to remote branches with different prefixes in git? 将所有远程 git 分支作为本地分支进行跟踪 - Track all remote git branches as local branches 如何删除 git repo 中的所有文件并从本地 git repo 更新/推送? - How can I remove all files in my git repo and update/push from my local git repo? 如果我的本地分支从 GIT 存储库中删除,我该如何清理它们 - How can I clean up my local branches if they are deleted from GIT repo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM