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

发布于 2024-11-04 17:54:06 字数 357 浏览 1 评论 0原文

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

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

假设如下:

  1. 因为他们分叉了我的项目,所以我们的两个存储库共享相同的“历史”。
  2. 尽管 GitHub 显示他们的项目是从我的项目分叉的,但我的本地存储库没有对此人的项目的任何引用。我需要将他们的添加为遥控器吗?
  3. 我还没有一个名为“foo”的分支 - 我不知道是否需要先手动创建它。
  4. 我绝对希望将其拉入一个单独的分支而不是我的主人。

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:

  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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

时光匆匆的小流年 2024-11-11 17:54:07

以下是一个很好的权宜解决方案,可与 GitHub 配合使用,从其他用户的分支中检出 PR 分支。您需要知道拉取请求 ID(GitHub 与 PR 标题一起显示)。

示例:

拉取请求 #8
alice 想要将 her_repo:branch 中的 1 个提交合并到 your_repo:master 中,

git checkout -b <branch>
git pull origin pull/8/head
  • 替换为分支name from her_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 from her_repo:branch

git checkout -b <branch>
git pull origin pull/8/head
  • Substitute <branch> with the branch name from her_repo:branch
  • Substitute the name of your remote if you called it something different from origin.
  • Substitute 8 with the correct pull request ID.
故笙诉离歌 2024-11-11 17:54:07

如果 antak 的答案:

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

给你:

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

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

那么(遵循 Przemek D 的建议)使用

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

If antak's answer:

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

git fetch https://github.com/<THEIR USERNAME>/<REPO>.git <THEIR BRANCH>:<OUR NAME FOR BRANCH>
梦旅人picnic 2024-11-11 17:54:07

如果分叉的存储库受到保护,因此您无法直接推送到它,并且您的目标是对其 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

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

git push --set-upstream origin foo

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

git checkout foo 
git fetch protected_repo
git merge protected_repo/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:

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.

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.

git checkout foo 
git fetch protected_repo
git merge protected_repo/foo
水染的天色ゝ 2024-11-11 17:54:07

更新:这似乎不再起作用,因为 Github 更改了他们的 UI,我们希望他们能够恢复它


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

  1. 滚动到底部PR 以查看合并压缩并合并 按钮
  2. 单击右侧的链接:查看命令行说明
  3. 按右侧的复制图标步骤 1
  4. 将命令粘贴到终端中

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:

  1. Scroll to the bottom of the PR to see the Merge or Squash and merge button
  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
你与清晨阳光 2024-11-11 17:54:07

我在尝试继续一位同事之前创建的 Azure DevOps Repos PR 时偶然发现了这一点。
我最终使用的方法:

<navigate to relevant directory aka. git repo on local machine>

git fetch --all
# Optional verification: git branch -a

git checkout -b <local-branch> <remote-branch>
# e.g: git checkout -b 007_Bondbranch remotes/origin/007_Bondbranch

# Optional verification: git remotes -v

# Continue normally via git add, git commit and git push

I stumbled upon this while trying to continue a Azure DevOps Repos PR a collegue had created earlier.
Approach I ended up using:

<navigate to relevant directory aka. git repo on local machine>

git fetch --all
# Optional verification: git branch -a

git checkout -b <local-branch> <remote-branch>
# e.g: git checkout -b 007_Bondbranch remotes/origin/007_Bondbranch

# Optional verification: git remotes -v

# Continue normally via git add, git commit and git push
怎言笑 2024-11-11 17:54:07

git fetchupstreampull//head:;

git fetch upstream pull/<pr_number>/head:<your_local_branch_name>

枫林﹌晚霞¤ 2024-11-11 17:54:06
git remote add coworker git://path/to/coworkers/repo.git
git fetch coworker
git checkout --track coworker/foo

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

git checkout foo
git pull

回复评论:

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

尽管我建议您创建新分支,但您不需要创建新分支。您也可以直接提交到 foo 并让您的同事拉取您的分支。但是该分支已经存在,并且您的分支 foo 需要设置为它的上游分支:

git branch --set-upstream foo colin/foo

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

git remote add colin git://path/to/colins/repo.git
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:

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"?

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 branch foo need to be setup as an upstream branch to it:

git branch --set-upstream foo colin/foo

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

git remote add colin git://path/to/colins/repo.git
孤城病女 2024-11-11 17:54:06

接受的答案有效,但您不需要将它们添加为遥控器,因为每次这样做都会很麻烦且痛苦。

获取他们的提交:

git fetch git://path/to/coworkers/repo.git theirbranch:ournameforbranch

这将创建一个名为 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 之类的操作来挑选他们的提交。

将其推回给他们:

通常,您想要修复他们的某些内容并将其推回给他们。这也是可能的:

git fetch git://path/to/coworkers/repo.git theirbranch
git checkout FETCH_HEAD

# fix fix fix

git push git://path/to/coworkers/repo.git HEAD:theirbranch

如果在分离状态下工作,则担心您一定要使用 :ournameforbranch 创建一个分支,并将上面的 FETCH_HEADHEAD 替换为 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:

git fetch git://path/to/coworkers/repo.git theirbranch:ournameforbranch

This creates a local branch named ournameforbranch which is exactly the same as what theirbranch was for them. For the question example, the last argument would be foo: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 like https://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 called FETCH_HEAD is available. You can git log FETCH_HEAD to see their commits then do things like cherry-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:

git fetch git://path/to/coworkers/repo.git theirbranch
git checkout FETCH_HEAD

# fix fix fix

git push git://path/to/coworkers/repo.git HEAD:theirbranch

If working in detached state worries you, by all means create a branch using :ournameforbranch and replace FETCH_HEAD and HEAD above with ournameforbranch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文