Git 获取远程分支

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

Git fetch remote branch

gitbranchgit-branchgit-fetch

提问by David

My colleague and I are working on the same repository. We've branched it into two branches, each technically for different projects, but they have similarities, so we'll sometimes want to commit back to the *masterfrom the branch.

我和我的同事正在同一个存储库上工作。我们已经将它分成了两个分支,每个分支在技术上都用于不同的项目,但它们有相似之处,所以我们有时会想要masterbranch.

However, I have the branch. How can my colleague pull that branch specifically?

但是,我有branch. 我的同事如何专门拉那个分支?

A git cloneof the repository does not seem to create the branches locally for him, though I can see them live on unfuddle after a push on my end.

git clone存储库的A似乎并没有为他在本地创建分支,尽管我可以看到它们在我的推动下继续存在。

Also, when I originally made the branch, I did -b checkout. Does that make much difference?

另外,当我最初创建分支时,我做了-b checkout. 这有很大区别吗?

$ git branch -r
origin/HEAD -> origin/master
origin/daves_branch
origin/discover
origin/master

$ git fetch origin discover
$ git checkout discover

These are the commands I ran. But it definitely is not working.

这些是我运行的命令。但这绝对行不通。

I want to be able to check out that branch and then push and commit back just the branches changes from various collaborators or workstations.

我希望能够检出该分支,然后只推送和提交来自各个协作者或工作站的分支更改

回答by ralphtheninja

You need to create a local branch that tracks a remote branch. The following command will create a local branch named daves_branch, tracking the remote branch origin/daves_branch. When you push your changes the remote branch will be updated.

您需要创建一个跟踪远程分支的本地分支。以下命令将创建一个名为daves_branch的本地分支,跟踪远程分支origin/daves_branch。当您推送更改时,远程分支将被更新。

For most recent versions of Git:

对于最新版本的 Git:

git checkout --track origin/daves_branch

--trackis shorthand for git checkout -b [branch] [remotename]/[branch]where [remotename] is originin this case and [branch] is twice the same, daves_branchin this case.

--trackgit checkout -b [branch] [remotename]/[branch][remotename]在这种情况下是起源的简写,而 [branch] 在这种情况下是daves_branch 的两倍。

For Git 1.5.6.5 you needed this:

对于 Git 1.5.6.5 你需要这个:

git checkout --track -b daves_branch origin/daves_branch

For Git 1.7.2.3 and higher, this is enough (it might have started earlier, but this is the earliest confirmation I could find quickly):

对于 Git 1.7.2.3 及更高版本,这已经足够了(它可能更早开始,但这是我能很快找到的最早确认):

git checkout daves_branch

Note that with recent Git versions, this command will not create a local branch and will put you in a 'detached HEAD' state. If you want a local branch, use the --trackoption.

请注意,对于最近的 Git 版本,此命令不会创建本地分支,而是会将您置于“分离的 HEAD”状态。如果您想要一个本地分支,请使用该--track选项。

Full details are here: 3.5 Git Branching - Remote Branches, Tracking Branches

完整细节在这里:3.5 Git 分支 - 远程分支,跟踪分支

回答by Mark Mikofski

I have used fetchfollowed by checkout...

我已经使用fetchcheckout...

git fetch <remote> <rbranch>:<lbranch>
git checkout <lbranch>

...where <rbranch>is the remote branch or source refand <lbranch>is the as yet non-existentlocal branch or destination refyou want to track and which you probably want to name the same as the remote branch or source ref. This is explained under optionsin the explanation of <refspec>.

...其中<rbranch>的远程分支或源REF并且<lbranch>是迄今不存在的当地分支机构或目的地裁判你想跟踪和你可能想名字一样的远程分支或源REF。这在解释中的选项下进行了解释<refspec>

Git is so smart it auto completes the first command if I tabafter the first few letters of the remote branch. That is, I don't even have to name the local branch, Git automatically copies the name of the remote branch for me. Thanks Git!

Git 非常聪明,如果我在远程分支的前几个字母后使用Tab 键,它会自动完成第一个命令。也就是说,我什至不必命名本地分支,Git 会自动为我复制远程分支的名称。谢谢吉特!

Also as the answer in this similar Stack Overflow postshows, if you don't name the local branch in fetch, you can still create it when you check it out by using the -bflag. That is,git fetch <remote> <branch>followed by git checkout -b <branch> <remote>/<branch>does exactly the same as my initial answer. And evidently, if your repository has only oneremote, then you can just do git checkout <branch>after fetchand it will create a local branch for you. For example,you just cloned a repository and want to check out additional branches from the remote.

同样,正如这篇类似的 Stack Overflow 帖子的答案所示,如果您没有在 中命名本地分支fetch,您仍然可以在使用-b标志签出时创建它。也就是说,git fetch <remote> <branch>其次git checkout -b <branch> <remote>/<branch>与我的初始答案完全相同。看样子,如果你的版本库只有一个遥控器,那么你可以这样做git checkout <branch>fetch,它会创建一个本地分支你。例如,您刚刚克隆了一个存储库,并想从远程检出其他分支。

I believe that some of the documentation for fetchmay have been copied verbatim from pull. In particular the section on <refspec>in optionsis the same. However, I do not believe that fetchwill ever merge, so that if you leave the destination side of the colon empty, fetchshould do nothing.

我相信 的某些文档fetch可能是从pull. 特别是<refspec>选项中的部分是相同的。但是,我不相信fetch会永远merge这样,如果您将冒号的目的地一侧留空,则fetch应该什么都不做

NOTE: git fetch <remote> <refspec>is short for git fetch <remote> <refspec>:which would therefore do nothing, but git fetch <remote> <tag>is the same as git fetch <remote> <tag>:<tag>which should copy the remote <tag>locally.

注意:git fetch <remote> <refspec>是它的缩写,git fetch <remote> <refspec>:因此什么都不做,但git fetch <remote> <tag>git fetch <remote> <tag>:<tag><tag>本地复制远程相同。

I guess this is only helpful if you want to copy a remote branch locally, but not necessarily check it out right away. Otherwise, I now would use the accepted answer, which is explained in detail in the first section of the checkout descriptionand later in the optionssection under the explanation of --track, since it's a one-liner. Well...sort of a one-liner, because you would stillhave to run git fetch <remote>first.

我想这仅在您想在本地复制远程分支时才有帮助,但不一定要立即查看。否则,我现在将使用已接受的答案,该答案结帐说明的第一部分以及稍后在说明下的选项部分中进行了详细说明--track,因为它是单行的。嗯......有点像单线,因为你仍然必须先运行git fetch <remote>

FYI: The order of the <refspecs>(source:destination) explains the bizarre pre Git 1.7 method for deleting remote branches. That is, push nothing into the destination refspec.

仅供参考:<refspecs>(source:destination)的顺序解释了用于删除远程分支的奇怪的 Git 1.7 之前的方法。也就是说,不将任何内容推送到目标 refspec 中。

回答by paneer_tikka

If you are trying to "checkout" a new remote branch (that exists only on the remote, but not locally), here's what you'll need:

如果您尝试“签出”一个新的远程分支(仅存在于远程分支上,而不存在于本地),那么您将需要以下内容:

git fetch origin
git checkout --track origin/<remote_branch_name>

This assumes you want to fetch from origin. If not, replace originby your remotename.

这假设您想从origin获取。如果没有,请用您的远程名称替换origin

回答by David

To checkout myBranch that exists remotely and not a locally - This worked for me:

要结帐远程存在而不是本地存在的 myBranch - 这对我有用:

git fetch --all
git checkout myBranch

I got this message:

我收到了这条消息:

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

回答by Daniel Lee

Use git branch -a(both local and remote branches) or git branch -r(only remote branches) to see all the remotes and their branches. You can then do a git checkout -t remotes/repo/branchto the remote and create a local branch.

使用git branch -a(本地和远程分支)或git branch -r(仅远程分支)查看所有远程及其分支。然后,您可以git checkout -t remotes/repo/branch对远程执行 a并创建一个本地分支。

There is also a git-ls-remotecommand to see all the refs and tags for that remote.

还有一个git-ls-remote命令可以查看该遥控器的所有引用和标签。

回答by Guillaume Vincent

The title and the question are confused:

标题和问题混淆了:

  • Git fetch remote branch
  • how can my colleague pull that branch specifically.
  • Git 获取远程分支
  • 我的同事怎么能专门拉那个分支。

If the question is, how can I get a remote branch to work with, or how can I Git checkout a remote branch?, a simpler solution is:

如果问题是,我如何才能使用远程分支,或者如何通过 Git 签出远程分支?,一个更简单的解决方案是:

With Git (>= 1.6.6) you are able to use:

使用 Git (>= 1.6.6) 你可以使用:

git checkout <branch_name>

If local <branch_name>is not found, but there does exist a tracking branch in exactly one remote with a matching name, treat it as equivalent to:

如果<branch_name>未找到本地,但在一个具有匹配名称的远程中确实存在跟踪分支,则将其视为等效于:

git checkout -b <branch_name> --track <remote>/<branch_name>

See documentation for Git checkout

请参阅 Git 检出文档

For your friend:

给你的朋友:

$ git checkout discover
Branch discover set up to track remote branch discover
Switched to a new branch 'discover'

回答by James Rochabrun

The easiest way to do it, at least for me:

最简单的方法,至少对我来说:

git fetch origin <branchName> # Will fetch the branch locally
git checkout <branchName> # To move to that branch

回答by ManuelSchneid3r

Use:

用:

git checkout -b serverfix origin/serverfix

This is a common enough operation that Git provides the --trackshorthand:

这是一个足够常见的操作,Git 提供了--track速记:

git checkout --track origin/serverfix

In fact, this is so common that there's even a shortcut for that shortcut. If the branch name you're trying to checkout (a) doesn't exist and (b) exactly matches a name on only one remote, Git will create a tracking branch for you:

事实上,这太常见了,以至于该快捷方式甚至还有一个快捷方式。如果您尝试签出的分支名称 (a) 不存在且 (b) 仅与一个远程上的名称完全匹配,则 Git 将为您创建一个跟踪分支:

git checkout serverfix

To set up a local branch with a different name than the remote branch, you can easily use the first version with a different local branch name:

要使用与远程分支不同的名称设置本地分支,您可以轻松地使用具有不同本地分支名称的第一个版本:

git checkout -b sf origin/serverfix

Now, your local branch sfwill automatically pull from origin/serverfix.

现在,您的本地分支sf将自动从origin/serverfix.

Source: Pro Git, 2nd Edition, written by Scott Chacon and Ben Straub(cut for readability)

资料来源:Pro Git,第 2 版,由 Scott Chacon 和 Ben Straub 编写(为了可读性而删减)

回答by Karthik damodara

With this simple command:

使用这个简单的命令:

git checkout -b 'your_branch' origin/'remote branch'

回答by Harshit Agarwal

To fetch a branch that exists on remote, the simplest way is:

要获取远程存在的分支,最简单的方法是:

git fetch origin branchName
git checkout branchName

You can see if it already exists on remote with:

您可以使用以下命令查看它是否已存在于远程:

git branch -r

This will fetch the remote branch to your local and will automatically track the remote one.

这会将远程分支提取到您的本地并自动跟踪远程分支。