Read Anthropic’s case study about Graphite Reviewer

Merging pull requests into multiple branches

Sara Verdi
Sara Verdi
Graphite software engineer
Try Graphite


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Merging pull requests into multiple branches can be a complex task, especially when you need to ensure consistency and minimize conflicts across your development streams. This guide will explore strategies for merging pull requests into multiple branches efficiently, using Git commands and Graphite's innovative Merge Queue feature.

In many development workflows, especially in larger projects, you may need to merge changes into multiple branches. Typically, this process involves opening a single pull request against a primary branch (like main or development), which is then reviewed and, upon approval, automatically merged into additional necessary branches through continuous integration (CI) tools. This ensures that all code undergoes review before integration, maintaining code quality and reducing the risk of conflicts.

The most common and efficient method for merging changes across multiple branches in modern development practices involves the following steps:

  1. Open a pull request: Developers push their changes to a feature branch and then open a pull request against a primary branch (e.g., development).
  2. Review and approval: The pull request is reviewed by team members. Once approved, it triggers the CI pipeline.
  3. Automated merging by CI: Configured CI tools automatically merge the approved changes into other required branches, like release or production, based on predefined rules or conditions set within the CI configuration.

This approach minimizes manual errors, streamlines workflows, and ensures that all merged code is reviewed and tested.

While automated processes are preferred for their efficiency and reliability, understanding manual merging strategies is valuable for cases where automated tools fail or when manual intervention is required.

To manually merge a feature branch into multiple branches, you typically perform the merge separately for each target branch:

  1. Check out the first target branch:
    Terminal
    git checkout development
  2. Merge the feature branch into the target branch:
    Terminal
    git merge feature1
  3. Repeat the steps for the next branch:
    Terminal
    git checkout release
    git merge feature1

If only specific commits are needed in multiple branches, git cherry-pick is a practical tool. This command applies the changes introduced by some existing commits to the current branch:

Terminal
git checkout release
git cherry-pick <commit-hash>

Graphite's Merge Queue enhances the merging process by handling stacks of changes across multiple branches more effectively, useful particularly in environments where the main branch is frequently broken or teams face delays due to rebasing changes.

  1. Stack-aware merging: The Merge Queue understands the dependencies between stacked pull requests. If you add a stack to the queue, it processes and validates the entire stack in parallel, potentially across different branches if configured accordingly.
  2. Fast-forward merges: Graphite supports a "fast-forward merge" option that can merge stacked PRs without needing to run continuous integration (CI) again, as the changes have already been validated.

To get started with the Merge Queue, configure it in your Graphite dashboard to specify which branches should be included in the merge strategies—either through a rebase or squash option—and enable the fast-forward merge for quicker processing.

While automated tools like CI and Graphite's Merge Queue are preferred for merging pull requests into multiple branches due to their efficiency and reduced risk of errors, understanding manual strategies remains useful for handling exceptional cases. This combination of automated and manual methods ensures that teams can maintain high standards of code integration across various development environments.

Built for the world's fastest engineering teams, now available for everyone