简体   繁体   English

如何使用git合并来自不同存储库的拉取请求(或提交)?

[英]How to merge a pull request (or commit) from a different repository, using git?

I have made a shallow clone (fork) of a GitHub repository A into my own repository B . 我已经将GitHub存储库A浅表克隆(分支)到我自己的存储库B中 Thus there is no commit history for B. However there is an unmerged (open) pull request X in repo A, that I would like to add to B, and still have it show up as a proper merged pull request, but without the addition of extra branches and added commit history. 因此,没有针对B的提交历史记录。但是,我要添加到B中的仓库A中有一个未合并的(打开的) 拉取请求 X ,我想将其添加到B中,并且仍然将其显示为适当的合并拉取请求,但是没有添加额外的分支和添加的提交历史记录。 (X is based on a particular commit Y in repo C on branch Z .) (X基于分支 Z上存储库C中的特定提交 Y。

How do you do this with git from CLI? 您如何使用CLI中的git做到这一点?


Given the seeming equivalence of merging a pull-request (from A) and picking a commit (from C), I guess there should be several different way to accomplish the same. 考虑到合并请求请求 (来自A)和选择提交 (来自C)的看似等效,我想应该有几种不同的方法来完成相同的任务。

I've looked at many similar questions, but they don't quite address the issue, since they often involve first fetching all commits in various ways, or are incomplete. 我已经看过许多类似的问题,但是它们并不能完全解决问题,因为它们通常涉及首先以各种方式获取所有提交,或者是不完整的。 I've looked at: 我看过:

This is how I did it. 这就是我做到的。

  1. Pull all the required changes from the repo in local: 从本地仓库中提取所有必需的更改:
    git pull . git pull

  2. Change the origin URL: 更改原始 URL:
    git remote set-url origin https://url.com . git remote set-url origin https://url.com

  3. Switch the branch you wanna push the code to: 将要推送代码的分支切换到:
    git checkout BRANCH or git checkout -b BRANCH git checkout BRANCHgit checkout -b BRANCH
    (I always get confused with this). (我总是对此感到困惑)。

  4. (optional) Set the upstream: (可选)设置上游:
    git branch --set-upstream-to=origin/BRANCH

  5. Stash: 藏:
    git stash

  6. Commit the changes: 提交更改:
    git commit -m "feat: moving branch"

  7. Finally push it all: 最后将其全部推送:
    git push

Hope this helps. 希望这可以帮助。 Let me know if you find any issues. 如果您发现任何问题,请告诉我。

Cherry picking a commit 樱桃采摘

In fact what I asked for is what is known as cherry picking a commit. 实际上,我要的是所谓的“ cherry picking However, because of the different commit histories of my own shallow repo B and the original commit Y in C , it is probably not possible (?) to have it look like a "proper merged pull request" , at least not in the sense of GitHubs Network graph shown. 但是,由于我自己的浅存储BC原始提交Y提交历史不同,所以可能不可能(?)使它看起来“适当的合并请求” ,至少在某种意义上显示了GitHubs Network图。
(If someone has another solution for this, please comment.) (如果有人对此有其他解决方案,请发表评论。)

The procedure is already well documented, and in this case, the most simple solution was: 该过程已被详细记录,在这种情况下,最简单的解决方案是:

git fetch git@github.com:<USERNAME>/<REPO-C> <BRANCH-Z> --no-tags
git cherry-pick <commit-Y>
git push origin master

This is by far the most easy way, as it doesn't involve having to add/change the fetch origins. 到目前为止,这是最简单的方法,因为它不需要添加/更改获取源。

Pull Specific PR with <id> 使用<id>拉取特定PR

You can also just pull the PR directly using the PR's id : 您也可以直接使用PR的ID直接拉出PR:

git pull https://github.com/{upstream/project} refs/pull/{id}/head

For other options, see: 有关其他选项,请参见:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM