简体   繁体   中英

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 . 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. (X is based on a particular commit Y in repo C on branch Z .)

How do you do this with git from CLI?


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.

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 .

  2. Change the origin URL:
    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
    (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. 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.
(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>

You can also just pull the PR directly using the PR's id :

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

For other options, see:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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