Renaming a branch while on pull request

GitGithubPull Request

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

"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.

Solution 2 - Git

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).

Solution 3 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMicheleView Question on Stackoverflow
Solution 1 - GitarbyleeView Answer on Stackoverflow
Solution 2 - GitSlobodan IlicView Answer on Stackoverflow
Solution 3 - GitVonCView Answer on Stackoverflow