如何从别人的仓库中提取远程分支
我在 GitHub 上托管了一个项目,有人已经分叉了该项目。在他们的分支上,他们创建了一个新分支“foo”并进行了一些更改。如何将他们的“foo”拉入我的存储库中也名为“foo”的新分支?
我知道他们可以向我提交拉取请求,但我想自己启动此流程。
假设如下:
- 因为他们分叉了我的项目,所以我们的两个存储库共享相同的“历史”。
- 尽管 GitHub 显示他们的项目是从我的项目分叉的,但我的本地存储库没有对此人的项目的任何引用。我需要将他们的添加为遥控器吗?
- 我还没有一个名为“foo”的分支 - 我不知道是否需要先手动创建它。
- 我绝对希望将其拉入一个单独的分支而不是我的主人。
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?
I understand they could submit a pull request to me, but I'd like to initiate this process myself.
Assume the following:
- Because they forked my project, both our repos share the same 'history'
- 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?
- I don't have a branch called "foo" yet - I don't know if I need to manually create this first.
- I definitely want this pulled into a separate branch and not my master.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
以下是一个很好的权宜解决方案,可与 GitHub 配合使用,从其他用户的分支中检出 PR 分支。您需要知道拉取请求 ID(GitHub 与 PR 标题一起显示)。
示例:
拉取请求 #8
alice 想要将
her_repo:branch
中的 1 个提交合并到your_repo:master
中,
替换为分支name fromher_repo:branch
origin
不同,请替换遥控器的名称。8
替换为正确的拉取请求 ID。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).
Example:
Pull request #8
alice wants to merge 1 commit into
your_repo:master
fromher_repo:branch
<branch>
with the branch name fromher_repo:branch
origin
.8
with the correct pull request ID.如果 antak 的答案:
给你:
那么(遵循 Przemek D 的建议)使用
If antak's answer:
gives you:
Then (following Przemek D's advice) use
如果分叉的存储库受到保护,因此您无法直接推送到它,并且您的目标是对其 foo 进行更改,那么您需要将其分支 foo 放入您的存储库中,如下所示:
现在您有 foo 的本地副本没有与之关联的上游。您可以对其进行更改(或不提交),然后将您的 foo 推送到您自己的远程存储库。
现在 foo 位于您在 GitHub 上的存储库中,并且您本地的 foo 正在跟踪它。如果他们继续对 foo 进行更改,您可以获取他们的更改并合并到您的 foo 中。
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:
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.
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.
更新:这似乎不再起作用,因为 Github 更改了他们的 UI,我们希望他们能够恢复它
GitHub 有一个相对于前面答案的新选项,只需从 PR 复制/粘贴命令行:
合并
或压缩并合并
按钮查看命令行说明
Update: this no longer seems to work, as Github changed their UI, let's hope they'll restore it
GitHub has a new option relative to the preceding answers, just copy/paste the command lines from the PR:
Merge
orSquash and merge
buttonview command line instructions
我在尝试继续一位同事之前创建的 Azure DevOps Repos PR 时偶然发现了这一点。
我最终使用的方法:
I stumbled upon this while trying to continue a Azure DevOps Repos PR a collegue had created earlier.
Approach I ended up using:
git fetchupstreampull//head:;
git fetch upstream pull/<pr_number>/head:<your_local_branch_name>
这将设置一个本地分支
foo
,跟踪远程分支coworker/foo
。因此,当您的同事进行了一些更改时,您可以轻松地拉动它们:回复评论:
尽管我建议您创建新分支,但您不需要创建新分支。您也可以直接提交到
foo
并让您的同事拉取您的分支。但是该分支已经存在,并且您的分支 foo 需要设置为它的上游分支:假设 colin 是您定义的存储库(远程到您的同事存储库)以类似的方式:
This will setup a local branch
foo
, tracking the remote branchcoworker/foo
. So when your co-worker has made some changes, you can easily pull them:Response to comments:
You don't need to create a new branch, even though I recommend it. You might as well commit directly to
foo
and have your co-worker pull your branch. But that branch already exists and your branchfoo
need to be setup as an upstream branch to it:assuming
colin
is your repository (a remote to your co-workers repository) defined in similar way:接受的答案有效,但您不需要将它们添加为遥控器,因为每次这样做都会很麻烦且痛苦。
获取他们的提交:
这将创建一个名为
ournameforbranch
的本地分支,它与他们的theirbranch
完全相同。对于问题示例,最后一个参数是 foo:foo 。git://path/to/coworkers/repo.git
部分应替换为其 GitHub 存储库页面上的 URL。 SSH URL 类似于[email protected]:theirusername/ reponame.git
和 HTTPS 之类的https://github.com/theirusername/reponame.git
。注意:如果想出一个不与您自己的分支之一冲突的名称很麻烦,则可以进一步省略
:ournameforbranch
部分。在这种情况下,可以使用名为FETCH_HEAD
的引用。您可以 git log FETCH_HEAD 来查看他们的提交,然后执行诸如 cherry-picked 之类的操作来挑选他们的提交。将其推回给他们:
通常,您想要修复他们的某些内容并将其推回给他们。这也是可能的:
如果在分离状态下工作,则担心您一定要使用
:ournameforbranch
创建一个分支,并将上面的FETCH_HEAD
和HEAD
替换为ournameforbranch
。The accepted answer works, but you don't need to add them as a remote, since that would be cumbersome and a pain to do each time.
Grabbing their commits:
This creates a local branch named
ournameforbranch
which is exactly the same as whattheirbranch
was for them. For the question example, the last argument would befoo:foo
.The
git://path/to/coworkers/repo.git
part should be replaced with the URL on their GitHub repository page. An SSH URL would look like[email protected]:theirusername/reponame.git
and an HTTPS one likehttps://github.com/theirusername/reponame.git
.Note
:ournameforbranch
part 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 calledFETCH_HEAD
is available. You cangit log FETCH_HEAD
to see their commits then do things likecherry-picked
to cherry pick their commits.Pushing it back to them:
Oftentimes, you want to fix something of theirs and push it right back. That's possible too:
If working in detached state worries you, by all means create a branch using
:ournameforbranch
and replaceFETCH_HEAD
andHEAD
above withournameforbranch
.