Programming
How to modify a pull request on GitHub to change target branch to merge into
Have you ever created a pull request on GitHub, only to realize you’ve targeted the wrong branch? It’s a common mistake, especially when working on multiple features or collaborating in a fast-paced environment. The good news is that modifying a pull request on GitHub to change the target branch to merge into is a relatively straightforward process. This guide will walk you through the steps to correct your pull request and ensure your code lands in the right place. We’ll cover everything from using the GitHub web interface to leveraging the command line for more advanced scenarios. Understanding how to adjust your pull request target branch is a crucial skill for efficient collaboration and maintaining a clean and organized codebase, and prevents accidental merges into incorrect environments, like pushing code meant for a development branch directly into production.
Why You Might Need to Change the Target Branch
There are several reasons why you might need to adjust the target branch of your pull request. Perhaps you initially selected the wrong branch due to a typo or misunderstanding of the project’s branching strategy. Maybe the feature you’re working on was originally intended for one release but has been re-prioritized for another. Or, it’s possible that the original target branch is no longer accepting changes, and a new branch has been created to replace it. Regardless of the reason, understanding how to modify a pull request on GitHub to change the target branch to merge into is essential for maintaining an efficient workflow.
Sometimes, a feature branch might be based on an older version of the code. By the time the feature is complete, the original target branch may have diverged significantly. In such cases, it’s crucial to re-target the pull request to a more up-to-date branch to avoid merge conflicts and ensure compatibility. According to a study by GitHub, pull requests with fewer merge conflicts are significantly more likely to be merged quickly. This highlights the importance of keeping your target branch aligned with the latest changes in the repository. Regularly rebasing or merging your feature branch with the target branch can also help minimize conflicts.
Consider a scenario where a developer is working on a new feature for version 2.0 of a software project. They initially create a pull request targeting the release/2.0 branch. However, during development, the team decides to postpone the feature to version 2.1. In this situation, the developer needs to modify the pull request on GitHub to change the target branch to merge into the release/2.1 branch. Failure to do so would result in the feature being merged into the wrong release, potentially causing issues and delaying the project’s timeline.
Changing the Target Branch via the GitHub UI
The easiest way to modify a pull request on GitHub to change the target branch to merge into is through the GitHub web interface. This method is ideal for simple changes and doesn’t require any command-line experience. It’s a user-friendly approach that allows you to quickly adjust your pull request’s destination.
To change the target branch via the GitHub UI, follow these steps:
- Navigate to your pull request on GitHub.
- Click on the “Edit” button next to the current target branch (usually located at the top of the pull request page).
- A dropdown menu will appear, listing all available branches in the repository.
- Select the new target branch from the dropdown menu.
- Click the “Change base branch” button to confirm your selection.
It’s important to note that changing the target branch can sometimes introduce merge conflicts if the new branch has diverged significantly from the original. GitHub will alert you if this is the case, and you’ll need to resolve the conflicts before the pull request can be merged. Resolving merge conflicts often involves manually editing the affected files to reconcile the differences between the branches. GitHub provides tools and guidance to assist with this process, making it easier to identify and resolve conflicts efficiently. This simple process ensures that your contributions end up in the intended destination.
Using the Command Line to Retarget a Pull Request
While the GitHub UI is convenient for simple changes, sometimes you need more control over the process. Using the command line allows you to modify a pull request on GitHub to change the target branch to merge into, especially when dealing with complex scenarios or when you need to update your local branch accordingly.
Here’s how you can change the target branch using the command line:
- First, fetch the latest changes from the remote repository: git fetch origin.
- Next, create a new branch based on the new target branch: git checkout -b new-feature-branch origin/new-target-branch. Replace “new-feature-branch” with a descriptive name for your new branch and “new-target-branch” with the actual name of the new target branch.
- Now, merge your existing feature branch into this new branch: git merge your-feature-branch.
- Resolve any merge conflicts that arise.
- Finally, push the new branch to the remote repository: git push origin new-feature-branch.
After pushing the new branch, you can update your pull request on GitHub to point to this new branch. Go to the pull request on the GitHub website, click on “Edit,” and select the new branch you just pushed. This method provides more flexibility and allows you to manage complex branching scenarios effectively. Remember to communicate these changes to your team to ensure everyone is aware of the updated target branch. Properly managing your git branches is crucial to success, and using the command line gives you the most control. Check out our other guides for more git tips.
For example, imagine you initially based your feature branch on develop, but the team has now decided to merge all new features into staging for the next release. You would use the command line to create a new branch based on staging, merge your feature branch into it, resolve any conflicts, and then update your pull request to target staging. This process ensures that your changes are integrated correctly into the desired environment.
Best Practices and Troubleshooting
When modifying a pull request on GitHub to change the target branch to merge into, it’s important to follow certain best practices to avoid potential issues and ensure a smooth workflow. Always communicate with your team before making significant changes to the target branch, especially if other developers are working on related features.
Here are some best practices to keep in mind:
- Communicate: Inform your team about the change in target branch to avoid confusion.
- Rebase Regularly: Keep your feature branch up-to-date with the target branch to minimize merge conflicts.
- Test Thoroughly: After changing the target branch, ensure that your code still works as expected in the new environment.
One common issue is encountering merge conflicts after changing the target branch. This usually happens when the new target branch has diverged significantly from the original. To resolve merge conflicts, you’ll need to manually edit the affected files and reconcile the differences between the branches. GitHub provides tools to help you identify and resolve these conflicts. Another potential issue is accidentally targeting the wrong branch again. Double-check your selection before confirming the change to avoid further complications. Always review the pull request after changing the target branch to ensure everything is correct. According to Atlassian, a well-defined branching strategy can significantly reduce the likelihood of merge conflicts and other integration issues [1](https://www.atlassian.com/git/tutorials/comparing-workflows/feature-branch-workflow).
If you’re consistently encountering issues when modifying a pull request on GitHub to change the target branch to merge into, consider using a more structured branching strategy, such as Gitflow [2](https://nvie.com/posts/a-successful-git-branching-model/). Gitflow provides a clear framework for managing different types of branches and can help prevent accidental merges into the wrong branches. Additionally, consider using a pull request template to remind developers to double-check the target branch before submitting a pull request. This simple step can help prevent many common errors and streamline the code review process.
- Can I change the target branch after the pull request has been approved?
- Yes, you can change the target branch even after the pull request has been approved. However, it's best to inform the approvers about the change, as it may require them to re-review the code.
- What happens to the comments and reviews if I change the target branch?
- The comments and reviews will remain associated with the pull request, even after you change the target branch. However, it's a good idea to address any new merge conflicts or issues that arise as a result of the change.
- Is there a way to prevent accidentally targeting the wrong branch?
- Yes, you can use a pull request template that includes a checklist to remind developers to double-check the target branch. You can also enforce branch protection rules to prevent direct commits to certain branches, such as the main or release branches.
It’s clear that knowing how to modify a pull request on GitHub to change the target branch to merge into is a valuable skill for any developer. Whether you prefer the simplicity of the GitHub UI or the power of the command line, the ability to adjust your pull request’s destination ensures your contributions are integrated correctly and efficiently. By following the best practices outlined in this guide, you can avoid common pitfalls and maintain a smooth and collaborative workflow. So, next time you find yourself with a pull request targeting the wrong branch, don’t panic! Use the techniques we’ve discussed to quickly and easily correct your mistake. Remember to communicate with your team, resolve any merge conflicts, and test your code thoroughly. By mastering this essential skill, you’ll contribute to a cleaner, more organized codebase and a more productive development process. If you found this helpful, check out our other articles on Git best practices and collaborative coding workflows [3](https://www.git-tower.com/learn/git/ebook/)。 Question & Answer :
I have a pull request that is requesting a merge into master from my branch, but the owner wants me to change the request to merge into a different branch from my branch.
Is this possible? What are the possible solutions?
Update August 2016: Change the base branch of a Pull Request finally allows for changing that branch.
(And this closes issue 18, which was 3 years old and had 1500+ comments)
After you’ve created a pull request, you can modify the base branch so that the changes in the pull request are compared against a different branch.
As noted in the comments byspazm:
It is (and was) working.
Changing target branch is not enabled while the PR is closed.
Solution was to “reopen” the PR (meaning by using the option in the UI) and then change the target branch
Original answer:
Since a PR cannot be edited in term of branch (see below), the easiest way would be to:
- create a local new branch on top of your current branch
- push that new branch
- make a new PR with the right destination.
- close the previous PR
- reference the “old” pull request from the new one; eg.
Supersedes #123(as commented below by Rivera)
(original answer, valid only when creating the PR)
You could try and chose another base branch, as in “Changing the branch range and destination repository” (Clicking on the Edit button at the top of a PR page)

The easiest way of thinking about the branch range is this:
- the base branch is where you think changes should be applied,
- the head branch is what you would like to be applied.
Changing the base repository changes who is notified of the pull request.
Everyone that can push to the base repository will receive an email notification and see the new pull request in their dashboard the next time they sign in.
If you select as a base branch the one that the original maintainer (the owner) wants, your PR should merge from your branch (unchanged) to the new base branch.
