Home > Back-end >  How to rebase a PR on a fork that isn't mine?
How to rebase a PR on a fork that isn't mine?

Time:10-07

There is a project :

slava/NiceProject

I'm the project maintainer.

Say a user forked my project so he has:

user/NiceProject

The user is raising PR from his fork into my project

user wants to merge 3 commits into slava:main from user:patch-1

Let's say that user is now not responding I want to merge the PR but it needs to be rebased. The user set allow edits from maintainers.

My problem is that I can't seem to do the push after rebase right.

This is what I did:

git checkout main
git pull --rebase
git checkout -b patch-1 main
git pull https://github.com/user/NiceProject.git patch-1
rebase main
git push -f  <---- problem is here

When I try to push it says:

fatal: The current branch patch-1 has no upstream branch.
To push the current branch and set the remote as upstream, use
    git push --set-upstream origin patch-1

The message is right I don't have patch-1 in origin. This is not my branch. doing git push --set-upstream origin patch-1 is not the right way because it will create this branch and push the changes from the user code. This is not what I want.

I want to push the changes into the user original branch in his fork so the original PR will be rebased. I don't want to create a new PR with a copy of the commits.

How can I do this?

CodePudding user response:

TL;DR Add the user's fork as an additional remote in your sandbox.

Details

When I have situations like this, I add the user's fork as an additional remote in my sandbox. I'm going to call it user-fork here, but I would normally use the actual user's GitHub name in the name, so I remember later where it came from.

git remote add user-fork https://github.com/user/NiceProject.git
git fetch user-fork

At this point, I checkout the branch patch-1 and by default it will be configured to track user-fork/patch-1 as its upstream:

git checkout patch-1

No you can edit the PR as you need. When you are done, just push it:

git push user-fork patch-1

Of course, this command will only work if the user gave you push permission on their PR's branch, as you say they did. In the comments, you added that this is the default when submitting a PR, so this should usually work.

Variant

In your question, you had already checked out a local patch-1 branch. If you want to keep that sandbox, set the upstream for that branch manually:

git checkout patch-1
git branch -u user-fork/patch-1

Or you can set the up-stream at the same time as you push:

git push -f --set-upstream user-fork patch-1

Cleaning up

When you're done with this PR, you can safely delete the remote to clean up your sandbox:

git remote remove user-fork

PS: Thanks for the question. I did not realize I might be able to do this. Instead, I've been discarding such PRs and redoing them myself, but it would be much nicer to fix the PR while keeping on record who submitted it and the rest of the metadata in the PR.

  • Related