Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Pull a commit from a different repository
  • Merge Pull Request from Upstream Branch into a Forked Repository
  • git: how to merge a pull request into a fork?
  • How do I merge a pull request on someone else's project in git?
like image 386
not2qubit Avatar asked Jul 31 '26 11:07

not2qubit


1 Answers

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 [email protected]:<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:

  • How to apply unmerged upstream pull requests from other forks into my fork?
like image 136
not2qubit Avatar answered Aug 03 '26 02:08

not2qubit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!