如何检出远程 Git 分支?

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

How do I check out a remote Git branch?

gitgit-checkoutremote-branch

提问by Juri Glass

Somebody pushed a branch called testwith git push origin testto a shared repository. I can see the branch with git branch -r.

有人将一个名为testwith的分支推git push origin test送到共享存储库。我可以看到分支git branch -r

Now I'm trying to check out the remote testbranch.

现在我正在尝试检查远程test分支。

I've tried:

我试过了:

  • git checkout testwhich does nothing

  • git checkout origin/testgives * (no branch). Which is confusing. How can I be on "no branch"?

  • git checkout test什么都不做

  • git checkout origin/test* (no branch). 这是令人困惑的。我怎么能在“没有分支”上?

How do I check out a remote Git branch?

如何检出远程 Git 分支?

回答by hallski

Update

更新

Jakub's answeractually improves on this. With Git versions ≥ 1.6.6, with only one remote, you can just do:

Jakub 的回答实际上改进了这一点。对于 Git 版本 ≥ 1.6.6,只有一个遥控器,您可以这样做:

git fetch
git checkout test

As user masukomi points out in a comment, git checkout testwill NOT work in modern git if you have multiple remotes. In this case use

正如用户 masukomi 在评论中指出的那样,git checkout test如果您有多个遥控器,将无法在现代 git 中工作。在这种情况下使用

git checkout -b test <name of remote>/test

or the shorthand

或速记

git checkout -t <name of remote>/test

Old Answer

旧答案

Before you can start working locally on a remote branch, you need to fetch it as called out in answers below.

在您开始在远程分支上本地工作之前,您需要按照以下答案中的说明获取它。

To fetch a branch, you simply need to:

要获取分支,您只需要:

git fetch origin

This will fetch all of the remote branches for you. You can see the branches available for checkout with:

这将为您获取所有远程分支。您可以通过以下方式查看可用于结帐的分支:

git branch -v -a

With the remote branches in hand, you now need to check out the branch you are interested in, giving you a local working copy:

有了远程分支,您现在需要检查您感兴趣的分支,为您提供本地工作副本:

git checkout -b test origin/test

回答by Jakub Nar?bski

Sidenote:With modern Git (>= 1.6.6), you are able to use just

旁注:使用现代 Git (>=1.6.6),您可以只使用

git checkout test

(note that it is 'test' not 'origin/test') to perform magical DWIM-mery and create local branch 'test' for you, for which upstream would be remote-tracking branch 'origin/test'.

(请注意,它是“测试”而不是“来源/测试”)执行神奇的DWIM-mery 并为您创建本地分支“测试”,上游将是远程跟踪分支“来源/测试”。



The * (no branch)in git branchoutput means that you are on unnamed branch, in so called "detached HEAD" state (HEAD points directly to commit, and is not symbolic reference to some local branch). If you made some commits on this unnamed branch, you can always create local branch off current commit:

* (no branch)git branch输出的意思是你在无名分支,在所谓的“分离的头”状态(HEAD直接点提交,并不是象征性的参考了一些当地的分支机构)。如果你在这个未命名的分支上做了一些提交,你总是可以在当前提交之外创建本地分支:

git checkout -b test HEAD

** EDIT (by editor notauthor) **

** 编辑(由编辑而非作者)**

I found a comment buried below which seems to modernize this answer:

我发现下面埋藏的一条评论似乎使这个答案现代化:

@Dennis: git checkout , for example git checkout origin/test results in detached HEAD / unnamed branch, while git checkout test or git checkout -b test origin/test results in local branch test (with remote-tracking branch origin/test as upstream) – Jakub Nar?bski Jan 9 '14 at 8:17

@Dennis: git checkout ,例如 git checkout origin/test 结果在分离的 HEAD / 未命名分支,而 git checkout test 或 git checkout -b test origin/test 结果在本地分支测试(远程跟踪分支 origin/test 作为上游) – Jakub Nar?bski 2014 年 1 月 9 日 8:17

emphasis on git checkout origin/test

强调 git checkout origin/test

回答by ndim

In this case, you probably want to create a local testbranch which is tracking the remote testbranch:

在这种情况下,您可能想要创建一个test跟踪远程test分支的本地分支:

$ git branch test origin/test

In earlier versions of git, you needed an explicit --trackoption, but that is the default now when you are branching off a remote branch.

在 的早期版本中git,您需要一个显式--track选项,但现在当您从远程分支分支时,这是默认选项。

回答by Corey Ballou

Accepted answernot working for you?

接受的答案对您不起作用?

While the first and selected answer is technically correct, there's the possibility you have not yet retrieved all objects and refs from the remote repository. If that is the case, you'll receive the following error:

虽然第一个和选定的答案在技术上是正确的,但您可能尚未从远程存储库中检索到所有对象和引用。如果是这种情况,您将收到以下错误:

$ git checkout -b remote_branch origin/remote_branch

fatal: git checkout: updating paths is incompatible with switching branches.
Did you intend to checkout 'origin/remote_branch' which can not be resolved as commit?

致命:git checkout:更新路径与切换分支不兼容。
您是否打算签出无法解析为提交的“origin/remote_branch”?

Solution

解决方案

If you receive this message, you must first do a git fetch originwhere originis the name of the remote repository prior to running git checkout remote_branch. Here's a full example with responses:

如果您收到此消息,您必须首先做一个git fetch origin地方origin是远程仓库之前运行的名称git checkout remote_branch。这是一个带有响应的完整示例:

$ git fetch origin
remote: Counting objects: 140, done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 69 (delta 36), reused 66 (delta 33)
Unpacking objects: 100% (69/69), done.
From https://github.com/githubuser/repo-name
   e6ef1e0..5029161  develop    -> origin/develop
 * [new branch]      demo       -> origin/demo
   d80f8d7..359eab0  master     -> origin/master

$ git checkout demo
Branch demo set up to track remote branch demo from origin.
Switched to a new branch 'demo'

As you can see, running git fetch originretrieved any remote branches we were not yet setup to track on our local machine. From there, since we now have a ref to the remote branch, we can simply run git checkout remote_branchand we'll gain the benefits of remote tracking.

如您所见,运行git fetch origin检索了我们尚未在本地机器上设置跟踪的任何远程分支。从那里开始,因为我们现在有一个远程分支的引用,我们可以简单地运行git checkout remote_branch,我们将获得远程跟踪的好处。

回答by Sahil kalra

I tried the above solution, but it didn't work. Try this, it works:

我尝试了上述解决方案,但没有奏效。试试这个,它有效:

git fetch origin 'remote_branch':'local_branch_name'

This will fetch the remote branch and create a new local branch (if not exists already) with name local_branch_nameand track the remote one in it.

这将获取远程分支并使用名称创建一个新的本地分支(如果尚不存在)local_branch_name并跟踪其中的远程分支。

回答by tacaswell

This will DWIMfor a remote not named origin (documentation):

这将为未命名的远程源(文档)进行DWIM

$ git checkout -t remote_name/remote_branch

To add a new remote, you will need to do the following first:

要添加新遥控器,您需要先执行以下操作:

$ git remote add remote_name location_of_remote
$ git fetch remote_name

The first tells Git the remote exists, the second gets the commits.

第一个告诉 Git 远程存在,第二个获取提交。

回答by matanster

Use:

用:

git checkout -b <BRANCH-NAME> <REMOTE-NAME>/<BRANCH-NAME>

Other answers do not work with modern Git in my benign case. You might need to pull first if the remote branch is new, but I haven't checked that case.

在我的良性案例中,其他答案不适用于现代 Git。如果远程分支是新的,您可能需要先拉取,但我没有检查过这种情况。

回答by Alireza

OK, the answer is easy... You basically see the branch, but you don't have a local copy yet!...

好的,答案很简单...您基本上看到了分支,但您还没有本地副本!...

You need to fetchthe branch...

你需要去fetch分行...

You can simply fetch and then checkout to the branch, use the one line command below to do that:

您可以简单地获取然后结帐到分支,使用下面的一行命令来做到这一点:

git fetch && git checkout test

I also created the image below for you to share the differences, look at how fetchworks and also how it's different to pull:

我还创建了下面的图像,供您分享差异,看看它是如何fetch工作的以及它与以下内容的不同之处pull

git fetch

混帐

回答by Madhan Ayyasamy

To clone a Git repository, do:

要克隆 Git 存储库,请执行以下操作:

git clone <either ssh url /http url>

The above command checks out all of the branches, but only the masterbranch will be initialized. If you want to checkout the other branches, do:

上面的命令会检查所有的分支,但只有master分支会被初始化。如果要结帐其他分支,请执行以下操作:

git checkout -t origin/future_branch (for example)

This command checks out the remote branch, and your local branch name will be same as the remote branch.

此命令检查远程分支,您的本地分支名称将与远程分支相同。

If you want to override your local branch name on checkout:

如果要在结帐时覆盖本地分支名称:

git checkout -t -b enhancement origin/future_branch

Now your local branch name is enhancement, but your remote branch name is future_branch.

现在您的本地分支名称是enhancement,但您的远程分支名称是future_branch

Documentation

文档

回答by uma

You can try

你可以试试

git fetch remote
git checkout --track -b local_branch_name origin/branch_name

or

或者

git fetch
git checkout -b local_branch_name origin/branch_name