'How to change the owner of a PR on GitHub / How to commandeer an open GitHub PR
I find this missing feature in GitHub to be frustrating, so I'm documenting my work-around here to help the next person. Alternate, better work-arounds are welcome.
This question is not a duplicate of How to change the author of a PR in GitHub? ...because that question isn't clear if it is asking about how to rewrite the author of a few commits and the push those to github, or actually change the name under which the entire PR was created in the first place. And, the accepted answer to that question was a simple fix to the local .git/config
file, which clearly will not solve the GitHub problem I'm talking about here.
At the top of a GitHub PR you'll see something like this:
username
wants to merge1
commit intobase_branch
fromtheir_feature_branch
Example PR (chosen "at random" from GitHub, just to show the PR author line in the image below). Image:
That username
: how can we change that? Example use-cases:
- The team-mate who opened this PR just left the company, and we'd like to commandeer (take over) and finish the PR for them.
- Change of work-loads have necessitated you take over a partially-complete PR from another team-mate. How can you switch that PR to be in your name?
Assume that everyone has full push access to the whole repo, meaning that you can push/pull to/from each other's branches anyway.
Solution 1:[1]
Sometimes, an assignment gets passed off from one team member to another, or, a team member leaves a team. When this happens, it would be nice to "commandeer", or take over, their PR so that it becomes your PR. As far as I can tell, however, this isn't possible on GitHub yet.
On Phabricator (a paid alternative to GitHub, and originally an internal tool used at Facebook), this is as simple as clicking a button to "Commandeer Revision" (see old documentation here under "Take over another author's change"). This is known as "commandeering someone's diff", where "diff" here is the Phabricator-equivalent to a GitHub PR, or "Pull Request".
How to commandeer (take over) someone else's PR in GitHub
ie: how to change the owner of the open PR so it looks like you opened the PR, not them.
So, since GitHub doesn't allow commandeering a PR, here are some options:
- Continue using their open PR, in which case their name, not yours, gets attached to the final, squashed-and-merged commit in the event you use the "Squash and merge" option to finish the PR. If they did the bulk of the work, that's fine. But, if you are taking over a PR and you are doing the bulk of the work, you'd probably like your name to be attached to the work. So, instead:
- Just close their open PR and open your own.
To do option 1 above: just keep using their open PR, in which their name gets attached to the final, squashed merge commit:
- Check out their branch locally
git fetch origin their_branch_name git checkout their_branch_name
- Optionally, rename your local copy of their branch to something you like
git branch -m new_branch_name
- Set the upstream for this branch so that when you
git push
it will push to their remote branch name which is attached to their open PR:
Note: I learned thegit push --set-upstream origin new_branch_name:their_branch_name
git push -u origin local_FROM_branch:remote_TO_branch
syntax here: How can I push a local Git branch to a remote with a different name easily? See also my own new answer to that question here. - Now, to push you can just call:
git push
- And to pull from that branch, in case another team-mate pushes changes to it too, you can specify:
git pull origin their_branch_name
Now, whenever the PR is complete and reviewed, you can merge it via GitHub. If you choose the regular merge option you'll get credit for your commits. If you choose the "squash and merge" option, the original author, NOT you gets full credit for the entire merge. This is dumb and should be fixed by GitHub, but, that's how it is.
Here's how to do option 2 above: just close their PR and open your own:
- Go to the bottom of their PR and click "Close pull request": .
- Check out their branch locally
git fetch origin their_branch_name git checkout their_branch_name
- Optionally, but recommended, rename your local copy of their branch to something you like.
git branch -m new_branch_name
- Push this as a new branch to the remote
origin
on GitHub. This pushes to your remote branch and allows you to open a NEW PR under YOUR name on GitHub:git push --set-upstream origin new_branch_name # Note: if you didn't rename the branch to `new_branch_name` above, # and it is therefore still called `their_branch_name` locally, just # use `their_branch_name` here instead.
- After pushing like that for the first time, GitHub will output a URL in the terminal where you pushed, which you can click on to open a new PR under your name. (If you don't have this feature, just go to Github.com and manually open up a PR there). Open a PR and voilá! It's now YOUR PR and you've just "commandeered" their PR!
- Now, to push you can just call:
git push
- And to pull from that branch, in case another team-mate pushes changes to it too, you can specify:
git pull origin new_branch_name
Now, when the PR is complete and reviewed, you can merge it on GitHub. If you choose the "squash and merge" option, your name will now be used for the final, single commit which gets merged to the base_branch
.
See also:
- How can I push a local Git branch to a remote with a different name easily?
- [my own new answer I just added there] How can I push a local Git branch to a remote with a different name easily?
Solution 2:[2]
Just giving An example when this ability will be a necessary feature: One of the PRs I Owned had state errors, that are not a part of the branch, but are localized to the PR:
>> git checkout -b new_name;
>> git push origin new_name;
>> opened new PR without errors.
The errors are not in GitHub per-se, but in some plugins and extensions we made for testing environment.
But I want the IT team to debug the state-corruption, so I would like to pass my PR onto them (the PR but not the code or the branch, obviously).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | gilad |