Reputation: 16920
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:
Upvotes: 7
Views: 8715
Reputation: 16920
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.
<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:
Upvotes: 7
Reputation: 3856
This is how I did it.
Pull all the required changes from the repo in local:
git pull
.
Change the origin URL:
git remote set-url origin https://url.com
.
Switch the branch you wanna push the code to:
git checkout BRANCH
or git checkout -b BRANCH
(I always get confused with this).
(optional) Set the upstream:
git branch --set-upstream-to=origin/BRANCH
Stash:
git stash
Commit the changes:
git commit -m "feat: moving branch"
Finally push it all:
git push
Hope this helps. Let me know if you find any issues.
Upvotes: 0