171

On Github, you can make pull requests to add functionality to a project. One's contributions have to be on a branch that, if the request is accepted, will be merged into the master branch (or an analogous one) of the project.

Now, I submitted a pull request on Github and my contributions are on a branch called patch-1. I can modify the name of the branch locally by

git branch -m patch-1 newname

and in principle I can also rename it on my forked repo on Github by following the instruction found in this answer. This is done in practice by removing the old branch, patch-1 in my case, and repush it with a different name newname.

Is it allowed to rename the branch patch-1 on my forked repository on Github when it constitutes a pull request? Or it causes problems on the pull request management?

Is there any way to rename a branch on a forked repository on Github when that branch is a pull request?

Community
  • 1
  • 1
Michele
  • 2,796
  • 2
  • 21
  • 29
  • 4
    This is now easier to do (since Jan. 2021). See [my answer below](https://stackoverflow.com/a/65799489/6309) – VonC Jan 19 '21 at 21:03
  • 1
    @VonC, not quite. That's about renaming the target branch (i.e. rename `master` to `main` will update all PR's that target `master` to now target `main`). The OP here asks about renaming the source branch of a PR. Even though Github has a "renaming feature" for this on the website, without any warning, it will close your PR when you do so, because your original branch will be deleted when you rename. – Abel Aug 06 '22 at 21:43
  • 2
    @Abel Yes, I believe the first comment under my answer made the same remark. This is not available yet. – VonC Aug 06 '22 at 21:50

3 Answers3

164

"Renaming" a remote branch in git, as indicated by the link you provided, is really just deleting a branch, followed by pushing a new one with the same commit hash but a new name. If you have a pull request open for branch patch-1, when you delete that branch, the pull request will be closed.

So, no you can't rename the branch with a pull request open without deleting the branch and removing the pull request. However, there's nothing stopping you from doing that, pushing a new branch with a new name, and creating a new pull request.

r3m0t
  • 1,850
  • 16
  • 21
arbylee
  • 1,959
  • 2
  • 12
  • 6
  • 283
    one reason for not doing so is losing the discussion on the existing PR. – Johnny Everson May 17 '16 at 20:37
  • 25
    I don't understand why such a tight restriction on renaming source branch on PR. Same annoying bahaviour exist in bitbucket. Another approach would be editing the PR and change the source branch to another branch. One might say "if you change the source branch, then it is a new PR anyways". Technically, yes, but also nothing prevents developer from setting upstream from a completely different branch and then do a `git push -f`. The PR is updated with an entire new code and still is the "same" PR. – L. Holanda Nov 28 '19 at 20:34
  • @L.Holanda The tight restriction makes perfect sense: When you rename a branch, you are in fact changing the topic of the discussion, so a new discussion on the new topic should start. Consider renaming `small-cosmetic-changes` to `fix-nasty-security-bug` - reviewing the PR should be done from an entirely different perspective, possibly by different stake holders. The name of the branch is, truly, a name of a topic. – Jaredo Mills Jan 31 '23 at 19:54
  • 10
    How about renaming from `small-cosmeitc-changes` to `small-cosmetic-changes`? Or from `fix-nasty-security-gub` to `fix-nasty-security-bug`? Tight restrictions on things that can always be made manually later with more steps are just non-sense. It's better to give both options as configuration and let the repository admin decide. My example with `git push -f` says it all, it's even worse than renaming a branch but still allowed. – L. Holanda Feb 09 '23 at 20:11
52

Update (Oct 2021):

Check out this answer

Original answer (Jan 2018):

Short Answer:

No

Alternative approach:

  1. Open a new PR with a new (renamed) branch
  2. Close the old PR referencing the new one (e.g. Closed in favor of #new_pr_id)
  3. Modify the description of the new PR (e.g. Supersedes #old_pr_id)
  4. (optional) Make a comment about the relevant discussion on the old PR

Note:

The name of a remote branch (constituting a PR) needed to change because the build system needed the branch's name that ends with the ticket ID. However, the PR was opened prior to official ticket creation (from specs) and had contained valuable discussion. The described approach was the only way to make build system work, and also not lose any information (although there was an extra step in tracking it).

Slobodan Ilic
  • 789
  • 8
  • 8
  • 12
    Given branches are typically deleted after being merged, I think it's better to "tolerate" the name being inaccurate for while than to add the overhead of a new PR that refers to the old PR for the purpose of tracking historical discussion. – Neo Jun 18 '19 at 10:03
  • 2
    Regarding "update" answer: I think the OP's intention is to change the "from" branch, not the base branch. I don't see how this is going to help. – David Cook Sep 24 '20 at 07:14
  • 2
    @SlobodanIlic can you remove the following outdated comment?, https://stackoverflow.com/questions/20007578/renaming-a-branch-while-on-pull-request#comment112870327_20007578 – Jaime Hablutzel Oct 15 '20 at 22:55
  • 1
    «branches typically deleted after merge» So what matters eventually is the merge commit, which _still_ says something like `Merge pull request #999 from wip-experimental-branch-try-something`, even though at that point, it's well tested and no longer experimental. – user2394284 Jun 04 '21 at 10:43
28

Short answer:

Yes for the target branch. Since Jan. 2021

Details

See "Support for renaming an existing branch".

rename branch dialog -- https://i2.wp.com/user-images.githubusercontent.com/2503052/105069955-a231fa80-5a50-11eb-982c-a114c9c44c57.png?ssl=1

Details in "How do I rename branch on the GitHub website?".

From github/renaming, this will re-target any open pull requests.

As charlie Harding adds in the comments:

  • "Will update 4 pull requests targeting this branch" means it will change the branch into which a pull request would like to merge.
    This was previously possible through those PRs’ pages anyway.
  • Trying to rename a branch which is the source of the PR, on the other hand, will result in that PR being closed ("Will close 1 open pull request for this branch.").

As Michael Freidgeim adds in the comments:

There is a request to support renaming base branch in github.community.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 38
    This is for renaming the wrong end of a pull request. “Will update 4 pull requests targeting this branch” means it will change the branch into which a pull request would like to merge. This was previously possible through those PRs’ pages anyway. Trying to rename a branch which is the source of the PR, on the other hand, will result in that PR being closed (“Will close 1 open pull request for this branch.”). – Charlie Harding Feb 12 '21 at 11:18
  • 1
    @CharlieHarding Thank you. I have amended the answer accordingly, and I have included your comment in said answer for more visibility. – VonC Feb 12 '21 at 11:37
  • 2
    There is a request to support renaming base branch in github.community https://github.community/t/update-pull-requests-from-renamed-branch/157676/3 – Michael Freidgeim Oct 02 '21 at 00:26
  • @MichaelFreidgeim Thank you. I have included your comment in the answer for more visibility. – VonC Oct 02 '21 at 12:57
  • @MichaelFreidgeim Unfortunately the link to the discussion on github community is dead... Are you able to retrieve it? – Michele Aug 03 '23 at 09:22
  • 1
    @Michele I have edited the answer and restored the community discussion link. – VonC Aug 03 '23 at 09:49