git 如何从别人的仓库中拉取远程分支

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

How to pull remote branch from somebody else's repo

gitgithubgit-branchgit-pullgit-remote

提问by Colin O'Dell

I've got a project hosted on GitHub which somebody has forked. On their fork, they've created a new branch "foo" and made some changes. How do I pull their "foo" into a new branch also named "foo" in my repo?

我有一个托管在 GitHub 上的项目,有人已经将其分叉了。在他们的 fork 上,他们创建了一个新分支“foo”并进行了一些更改。如何将他们的“foo”拉入我的回购中也名为“foo”的新分支?

I understand they could submit a pull request to me, but I'd like to initiate this process myself.

我知道他们可以向我提交拉取请求,但我想自己启动这个过程。

Assume the following:

假设如下:

  1. Because they forked my project, both our repos share the same 'history'
  2. Although GitHub shows their project was forked from mine, my local repository doesn't have any references to this person's project. Do I need to add theirs as a remote?
  3. I don't have a branch called "foo" yet - I don't know if I need to manually create this first.
  4. I definitely want this pulled into a separate branch and not my master.
  1. 因为他们分叉了我的项目,所以我们的两个仓库共享相同的“历史”
  2. 尽管 GitHub 显示他们的项目是从我的分叉出来的,但我的本地存储库没有对这个人的项目的任何引用。我需要将他们的添加为遥控器吗?
  3. 我还没有名为“foo”的分支 - 我不知道是否需要先手动创建它。
  4. 我绝对希望它被拉到一个单独的分支而不是我的主人。

回答by ralphtheninja

git remote add coworker git://path/to/coworkers/repo.git
git fetch coworker
git checkout --track coworker/foo

This will setup a local branch foo, tracking the remote branch coworker/foo. So when your co-worker has made some changes, you can easily pull them:

这将设置一个本地分支foo,跟踪远程分支coworker/foo。因此,当您的同事进行了一些更改时,您可以轻松地拉取它们:

git checkout foo
git pull


Response to comments:

对评论的回应:

Cool :) And if I'd like to make my own changes to that branch, should I create a second local branch "bar" from "foo" and work there instead of directly on my "foo"?

酷 :) 如果我想对那个分支进行自己的更改,我应该从“foo”创建第二个本地分支“bar”并在那里工作而不是直接在我的“foo”上工作?

You don't need to create a new branch, even though I recommend it. You might as well commit directly to fooand have your co-worker pull your branch. But that branch already exists and your branch fooneed to be setup as an upstream branch to it:

即使我推荐它,您也不需要创建新分支。您不妨直接提交foo并让您的同事拉取您的分支。但是该分支已经存在,您的分支foo需要设置为它的上游分支:

git branch --set-upstream foo colin/foo

assuming colinis your repository (a remote to your co-workers repository) defined in similar way:

假设colin您的存储库(您的同事存储库的远程)以类似方式定义:

git remote add colin git://path/to/colins/repo.git

回答by antak

No, you don't need to add them as a remote.That would be clumbersome and a pain to do each time.

不,您不需要将它们添加为遥控器。每次这样做都会很麻烦而且很痛苦。

Grabbing their commits:

抓取他们的提交:

git fetch [email protected]:theirusername/reponame.git theirbranch:ournameforbranch

This creates a local branch named ournameforbranchwhich is exactly the same as what theirbranchwas for them. For the question example, the last argument would be foo:foo.

这将创建一个本地分支,其名称与ournameforbranch它们的名称完全相同theirbranch。对于问题示例,最后一个参数是foo:foo

Note :ournameforbranchpart can be further left off if thinking up a name that doesn't conflict with one of your own branches is bothersome. In that case, a reference called FETCH_HEADis available. You can git log FETCH_HEADto see their commits then do things like cherry-pickedto cherry pick their commits.

:ournameforbranch如果想出一个与您自己的分支之一不冲突的名称很麻烦,则可以进一步省略注意部分。在这种情况下,调用的引用FETCH_HEAD可用。您可以git log FETCH_HEAD查看他们的提交,然后执行诸如cherry-picked挑选他们的提交之类的操作。

Pushing it back to them:

把它推回给他们:

Oftentimes, you want to fixsomething of theirs and push it right back. That's possible too:

通常,您想修复他们的某些东西并将其推回原处。这也是可能的:

git fetch [email protected]:theirusername/reponame.git theirbranch
git checkout FETCH_HEAD

# fix fix fix

git push [email protected]:theirusername/reponame.git HEAD:theirbranch

If working in detached stateworries you, by all means create a branch using :ournameforbranchand replace FETCH_HEADand HEADabove with ournameforbranch.

如果工作中游离状态担心你,用一切手段创建一个分支使用:ournameforbranch和替换FETCH_HEADHEAD上面ournameforbranch

回答by Peter

If antak's answer:

如果 antak 的回答是:

git fetch [email protected]:<THEIR USERNAME>/<REPO>.git <THEIR BRANCH>:<OUR NAME FOR BRANCH> 

gives you:

给你:

Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Then (following Przemek D's advice) use

然后(按照 Przemek D 的建议)使用

git fetch https://github.com/<THEIR USERNAME>/<REPO>.git <THEIR BRANCH>:<OUR NAME FOR BRANCH>

回答by Subfuzion

The following is a nice expedient solution that works with GitHub for checking out the PR branch from another user's fork. You need to know the pull request ID (which GitHub displays along with the PR title).

下面是一个很好的权宜之计,它与 GitHub 一起使用,用于从另一个用户的分支中检出 PR 分支。您需要知道拉取请求 ID(GitHub 与 PR 标题一起显示)。

Example:

例子:

Fixing your insecure code#8
alicewants to merge 1 commit into your_repo:masterfrom her_repo:branch

修复你的不安全代码#8
alice想要将 1 个提交合并到your_repo:masterfromher_repo:branch

git checkout -b <branch>
git pull origin pull/8/head

Substitute your remote if different from origin.
Substitute 8with the correct pull request ID.

如果与origin.
替换8为正确的拉取请求 ID。

回答by Robert Monfera

GitHub has a new option relative to the preceding answers, just copy/paste the command lines from the PR:

GitHub 有一个相对于前面的答案的新选项,只需从 PR 复制/粘贴命令行:

  1. Scroll to the bottom of the PR to see the Mergeor Squash and mergebutton
  2. Click the link on the right: view command line instructions
  3. Press the Copy icon to the right of Step 1
  4. Paste the commands in your terminal
  1. 滚动到 PR 底部以查看MergeSquash and merge按钮
  2. 点击右侧链接: view command line instructions
  3. 按步骤 1 右侧的复制图标
  4. 在终端中粘贴命令

回答by J Smith

If the forked repo is protected so you can't push directly into it, and your goal is to make changes to their foo, then you need to get their branch foo into your repo like so:

如果分叉的回购受到保护,因此您无法直接推入其中,并且您的目标是更改他们的 foo,那么您需要将他们的分支 foo 放入您的回购中,如下所示:

git remote add protected_repo https://github.com/theirusername/their_repo.git
git fetch protected_repo 
git checkout --no-track protected_repo/foo

Now you have a local copy of foo with no upstream associated to it. You can commit changes to it (or not) and then push your foo to your own remote repo.

现在您有一个 foo 的本地副本,没有与之关联的上游。您可以提交更改(或不提交),然后将您的 foo 推送到您自己的远程存储库。

git push --set-upstream origin foo

Now foo is in your repo on GitHub and your local foo is tracking it. If they continue to make changes to foo you can fetch theirs and merge into your foo.

现在 foo 位于您在 GitHub 上的存储库中,并且您的本地 foo 正在跟踪它。如果他们继续对 foo 进行更改,您可以获取他们的更改并合并到您的 foo 中。

git checkout foo 
git fetch protected_repo
git merge protected_repo/foo