Renaming a branch while on pull request
Categories:
How to Rename a Git Branch While on a Pull Request

Learn the correct and safe procedures for renaming a Git branch that is currently associated with an open Pull Request on GitHub.
Renaming a Git branch is a common task, but it can become tricky when that branch is already part of an active Pull Request (PR) on platforms like GitHub. Directly renaming the local branch without proper synchronization can lead to confusion and broken links in your PR. This article will guide you through the steps to safely rename your branch, update your remote repository, and ensure your Pull Request remains functional.
Understanding the Challenge
When you create a Pull Request, it's typically linked to a specific branch in your remote repository. If you simply rename your local branch and push it, Git will treat the renamed branch as a new, separate branch. The original branch name will still exist on the remote, and your PR will continue to point to it, leading to a disconnected state. The key is to not only rename the local branch but also to update the remote reference and, if necessary, inform the PR system about the change.
flowchart TD A[Start: Local Branch 'feature-old'] --> B{Create PR on GitHub} B --> C[PR linked to 'feature-old' on remote] C --> D{User renames local branch to 'feature-new'} D --> E{Push 'feature-new' to remote} E --> F{Delete 'feature-old' from remote} F --> G[PR still points to 'feature-old' (now deleted)] G --> H(Problem: PR is broken/disconnected) H --> I[Solution: Update PR to point to 'feature-new']
Flowchart illustrating the problem of renaming a branch without updating the PR.
Step-by-Step Guide to Renaming a Branch with an Active PR
Follow these steps carefully to rename your branch and update your Pull Request without issues. This process involves renaming locally, pushing the new branch, deleting the old remote branch, and finally updating the PR on GitHub.
1. 1. Ensure you are on the branch to be renamed
Before doing anything, make sure your local Git repository is checked out to the branch you intend to rename. Replace feature-old-name
with the actual name of your branch.
2. 2. Rename the local branch
Use the git branch -m
command to rename your local branch. The -m
flag stands for 'move' or 'rename'.
3. 3. Delete the old branch from the remote
Now that your local branch is renamed, you need to remove the old branch name from your remote repository. This is crucial to avoid confusion and ensure the PR can be updated correctly. The :feature-old-name
syntax tells Git to delete the remote branch.
4. 4. Push the new branch to the remote
Push your newly named local branch to the remote. The -u
(or --set-upstream-to
) flag sets the upstream tracking reference for your new local branch, so future git push
and git pull
commands will work as expected.
5. 5. Update your Pull Request on GitHub
After pushing the new branch and deleting the old one, navigate to your Pull Request on GitHub. GitHub is usually smart enough to detect that the original branch has been deleted and a new one with similar content has been pushed. It will often provide a prompt or an option to change the base branch of the PR to the new branch name. Look for an 'Edit' button next to the branch name or a banner suggesting an update. Select your feature-new-name
as the head branch.
git checkout feature-old-name
git branch -m feature-new-name
git push origin :feature-old-name
git push --set-upstream origin feature-new-name
Complete sequence of Git commands to rename a branch and update remote.
Verifying the Change
Once you've completed all the steps, go back to your Pull Request on GitHub. Confirm that the PR now correctly points to feature-new-name
. All commits and discussions should remain intact, as the PR's history is tied to the commit history, not just the branch name.
feature-old-name
branch, they will need to update their local repositories. They should fetch the latest changes, delete their local feature-old-name
branch, and then checkout the feature-new-name
branch.