Back to all articles

What is Github Merge Queue? | A Quick Overview

By The Trunk TeamAugust 29, 2024
merge

GitHub's merge queue is a handy tool for teams dealing with many pull requests. It helps manage the flow of code changes, making sure everything runs smoothly. If you want to avoid code conflicts and keep your main branch stable, a merge queue can be a lifesaver.

What is a GitHub Merge Queue?

Definition of GitHub merge queue: A GitHub merge queue is a system that organizes and manages pull requests waiting to be merged into a branch. It ensures that each pull request is merged in an orderly fashion and only after passing all required checks. This automation helps maintain a clean and stable branch without manual intervention.

Importance for high-performance teams: High-performance teams often deal with numerous pull requests daily. A merge queue becomes essential because it:

  • Automates the merging process.

  • Prevents bottlenecks in development.

  • Ensures that the main branch remains stable and up-to-date.

GitHub's merge queue can handle the complexities of multiple pull requests, allowing teams to focus on writing and improving code.

How it avoids code conflicts: The merge queue helps avoid code conflicts by:

  • Running all required checks before merging any pull request.

  • Creating temporary branches to test changes against the latest version of the base branch.

  • Grouping pull requests and validating them together to ensure compatibility.

This process means conflicts are caught early, and only conflict-free code gets merged, keeping the codebase healthy.

General availability and target users: GitHub merge queues are available for:

  • Public repositories owned by organizations.

  • Private repositories owned by organizations using GitHub Enterprise Cloud.

This feature targets teams that handle a high volume of pull requests, especially those in large organizations or open-source projects with many contributors. It ensures that even with numerous pull requests, the main branch remains stable and conflict-free.

Why Use a GitHub Merge Queue?

Benefits of increased development velocity: Using a GitHub merge queue can significantly speed up your development process. By automating the merging of pull requests, the queue helps teams avoid the manual task of checking and merging each request. This automation leads to:

  • Faster integration of changes.

  • Reduced waiting time for developers.

  • Continuous deployment with fewer interruptions.

The result? Developers can focus more on coding and less on administrative tasks, boosting overall productivity.

Ensuring branches are never broken by incompatible changes: Incompatible changes can break a branch, causing major setbacks. The merge queue addresses this by:

  • Running all required checks before merging.

  • Creating temporary branches to test changes against the latest code.

  • Rejecting any pull request that fails the checks.

This process ensures that only compatible and verified changes make it to the main branch, preventing potential disruptions in the workflow.

Comparison with traditional branch protection checks: Traditional branch protection checks require each pull request to be updated with the latest base branch changes before merging. This method can be time-consuming and repetitive. The merge queue, however, offers a more streamlined approach:

  • Traditional Checks: Require manual updates and waiting for status checks.

  • Merge Queue: Automatically updates pull requests and runs all checks in one go.

By eliminating the need for manual updates and reducing the waiting period, the merge queue offers a more efficient and user-friendly alternative to traditional branch protection checks.

Use cases for high-volume pull request branches: The merge queue shines in scenarios with a high volume of pull requests. Here are some specific use cases:

  • Open-source projects: With contributions from many developers, managing pull requests can become chaotic. The merge queue ensures an orderly process.

  • Large organizations: Teams working on large codebases with numerous pull requests benefit from the automated handling and reduced risk of conflicts.

  • Continuous integration/continuous deployment (CI/CD) environments: The merge queue supports seamless integration and deployment, aligning perfectly with CI/CD practices.

In these high-activity environments, the merge queue not only maintains stability but also accelerates the overall development cycle, making it an invaluable tool for modern software development practices.

Configuring Continuous Integration (CI) Workflows for Merge Queues

Using GitHub Actions

Setting up the merge_group event: To use GitHub Actions with a GitHub merge queue, you must configure the merge_group event. This event triggers workflows when a pull request enters the merge queue. The setup involves updating your workflow files:

  1. Navigate to your GitHub repository.

  2. Open the .github/workflows directory.

  3. Edit the desired workflow YAML file to include the merge_group event:

1on:
2 merge_group:
3 types:
4 - checks_requested

This configuration ensures that every time a pull request joins the queue, GitHub Actions will automatically run the necessary checks.

Example of a workflow triggering merge group checks: Here's a practical example of how to set up a workflow that triggers when a pull request is added to the merge queue:

1name: CI for Merge Queue
2
3on:
4 merge_group:
5 types:
6 - checks_requested
7
8jobs:
9 build:
10 runs-on: ubuntu-latest
11 steps:
12 - name: Checkout code
13 uses: actions/checkout@v2
14
15 - name: Set up Node.js
16 uses: actions/setup-node@v2
17 with:
18 node-version: '14'
19
20 - name: Install dependencies
21 run: npm install
22
23 - name: Run tests
24 run: npm test

This workflow checks out the code, sets up the Node.js environment, installs dependencies, and runs tests. These steps ensure that any code changes in the pull request pass all necessary checks before merging.

Using Third-Party CI Providers

Configuring CI for branches with gh-readonly-queue prefix: When using third-party CI providers, you must configure them to recognize branches created by the merge queue. These branches have a special prefix: gh-readonly-queue/{base_branch}. Follow these steps:

  1. Update your CI configuration to match branches with the prefix gh-readonly-queue/.

  2. Ensure your CI pipeline runs when these prefixed branches are pushed.

For example, if you're using a CI tool like CircleCI, your configuration might look like this:

1version: 2.1
2
3jobs:
4 build:
5 docker:
6 - image: circleci/node:14
7 steps:
8 - checkout
9 - run: npm install
10 - run: npm test
11
12workflows:
13 version: 2
14 merge_queue:
15 jobs:
16 - build:
17 filters:
18 branches:
19 only: gh-readonly-queue/**

This setup ensures that the CI pipeline runs checks on the temporary branches created by the merge queue.

Differences between GitHub Actions and third-party CI: While both GitHub Actions and third-party CI providers can handle merge queues, there are some key differences:

  • Integration: GitHub Actions integrates seamlessly with GitHub, offering a more streamlined experience. Third-party CI tools require additional configuration to recognize the special merge queue branches.

  • Event Triggers: GitHub Actions uses the merge_group event, specifically designed for merge queues. Third-party CI providers rely on branch name patterns to trigger builds.

  • Ease of Use: GitHub Actions provides a more straightforward setup for merge queues within the GitHub ecosystem. Third-party CI tools offer flexibility but may require more complex configurations.

By understanding these differences, you can choose the best CI solution for your workflow, ensuring that your merge queue operates efficiently and effectively.

Managing Pull Requests in a Merge Queue

Adding a pull request to the merge queue: To add a pull request (PR) to the GitHub merge queue, follow these steps:

  1. Open the pull request on GitHub.

  2. Click the "Merge" button, but instead of merging directly, select the option to add to the merge queue. Look for a button or link labeled "Merge when ready."

  3. Confirm the action by clicking "Confirm merge when ready." This step adds the PR to the merge queue, ensuring it will merge once all checks pass.

By adding a PR to the merge queue, you allow GitHub to handle the merging process automatically, ensuring that all necessary tests and checks are completed.

Ensuring pull request changes pass all required checks: For a pull request to merge successfully, it must pass all required checks. GitHub ensures this by running continuous integration (CI) workflows and other status checks.

  • CI Workflows: These workflows run automated tests and builds. You must configure these workflows to trigger on the merge_group event or the special branch prefix for third-party CI providers.

  • Status Checks: These include code quality checks, security scans, and other automated processes. GitHub will not merge the PR unless all status checks report success.

Handling merge conflicts and CI failures: Occasionally, a pull request might face merge conflicts or fail CI checks. Here's how to handle these issues:

  • Merge Conflicts: If a PR conflicts with the base branch or other PRs, GitHub removes it from the merge queue. The PR author must resolve conflicts manually by rebasing or merging the latest changes from the base branch.

  • CI Failures: If CI checks fail, the PR also exits the merge queue. The author must address the issues causing the failure, such as fixing broken tests or correcting code quality problems.

The pull request timeline will display the specific reasons for removal from the merge queue, helping authors understand and fix the issues.

Options for jumping to the top of the queue: In some cases, you might need a PR to merge quickly. GitHub offers an option to jump to the top of the merge queue.

  • Jump Option: When adding a PR to the merge queue, select the option to prioritize it. This action moves the PR to the front of the queue, triggering immediate checks.

  • Considerations: Be aware that jumping the queue can slow down the overall merge process for other PRs. It forces a rebuild of all in-progress PRs, as the reordering affects the commit graph.

Using the jump option sparingly ensures that the merge queue remains efficient and fair for all contributors.

By effectively managing pull requests in the GitHub merge queue, you can streamline the merging process, reduce conflicts, and maintain a stable codebase. This approach helps development teams work more efficiently, even with high volumes of pull requests.

Advanced Merge Queue Settings

Merge Methods

Choosing the right merge method is crucial for maintaining a clean and understandable project history. GitHub offers several merge methods—each with its own advantages and potential drawbacks.

  • Merge: This is the default method. When you use a merge commit, GitHub creates a new commit that combines the changes from the pull request with the base branch. This method preserves the complete history of changes, which can be helpful for tracking the evolution of the code. However, it can sometimes lead to a cluttered commit history, especially if there are many small or trivial commits.

  • Rebase: Rebasing moves the entire branch to the tip of the base branch, creating a linear history. This method can make the project history easier to follow because it avoids the clutter of merge commits. However, rebasing can be risky, especially if multiple developers are working on the same branch, as it rewrites commit history and may cause conflicts.

  • Squash: Squashing combines all commits from the pull request into a single commit. This method is useful for keeping the commit history clean and concise. It’s particularly effective for pull requests with many small commits that don't need to be tracked individually. However, squashing can make it harder to see the individual steps taken to reach the final change.

Build Concurrency and Limits

Managing build concurrency and setting limits are essential for optimizing the efficiency and performance of the merge queue. These settings help control the resources used during the continuous integration (CI) process and ensure smooth, timely merges.

  • Setting Maximum Concurrent CI Builds: You can specify the maximum number of CI builds that run simultaneously. This setting, known as build concurrency, helps manage the load on your CI infrastructure. For instance, setting a high concurrency limit allows multiple pull requests to be tested and merged at the same time, speeding up the overall process. However, setting this limit too high may overwhelm your CI resources, leading to slower builds and potential bottlenecks.

  • Defining Merge Limits and Timeout Settings: Merge limits determine how many pull requests can merge into the base branch at once. You can set both minimum and maximum merge limits:

    • Maximum Merge Limit: Useful for controlling the number of changes deployed at one time. For example, if your deployment process is resource-intensive or risky, you might set a low maximum limit to ensure stability.  

    • Minimum Merge Limit: Helps ensure that CI resources are used efficiently. For long-running builds, setting a minimum limit allows multiple pull requests to be grouped together, reducing the overall build time.

  • Timeout Settings: Define how long the merge queue should wait for CI checks to complete. If a check takes too long, the merge process will assume it has failed and remove the pull request from the queue. Setting appropriate timeout values helps prevent the queue from stalling due to slow or unresponsive CI checks.

By configuring these advanced settings, you can fine-tune the behavior of the GitHub merge queue to best suit your team's workflow and resource constraints. This ensures a smooth, efficient merge process, even when dealing with a high volume of pull requests.

How GitHub Merge Queues Work

Understanding how GitHub merge queues operate helps you optimize your workflow and avoid common pitfalls. Let's dive into the mechanics of GitHub merge queues.

First-in-First-Out Order of Merging Pull Requests

GitHub merge queues follow a first-in-first-out (FIFO) order. This means that pull requests are merged in the order they are added to the queue. By maintaining this order, GitHub ensures fairness and predictability in the merging process. The FIFO approach prevents newer pull requests from jumping ahead of older ones unless explicitly re-ordered, which maintains a logical sequence of changes.

Creation of Temporary Branches for Validation

When you add a pull request to the merge queue, GitHub automatically creates a temporary branch. This branch combines the changes from the pull request with the latest version of the target branch. This temporary branch is used to run CI checks, ensuring that merging the pull request will not introduce any issues. The use of temporary branches allows for thorough validation without affecting the main branch until all checks are complete.

Grouping Changes from Multiple Pull Requests

GitHub merge queues can group changes from multiple pull requests into a single group. This feature is particularly useful for high-volume repositories where multiple pull requests are submitted frequently. Grouping allows for combined CI checks, which can save time and resources. For example, if three pull requests are grouped, the merge queue will create a temporary branch containing changes from all three, run the CI checks, and merge them together if all checks pass. This reduces the number of individual CI runs and speeds up the merging process.

Process of Merging Successful and Removing Failing Pull Requests

The merge queue has a clear process for handling both successful and failing pull requests:

  • Merging Successful Pull Requests: If the temporary branch created for a pull request passes all required checks, GitHub proceeds to merge the pull request into the base branch. The merge queue then moves on to the next pull request in the queue. This ensures that only changes that have passed validation are merged into the main branch.

  • Removing Failing Pull Requests: If a pull request fails any of the required checks, it is removed from the queue. The pull request's timeline will show the reason for the failure, allowing developers to address the issues. GitHub then re-creates the temporary branch without the failing pull request and re-runs the CI checks for the remaining pull requests in the group. This process ensures that failing changes do not block the queue or affect the stability of the main branch.

By understanding these mechanics, you can better utilize the GitHub merge queue to maintain a stable and efficient development workflow. This structured approach to handling pull requests reduces conflicts, saves time, and ensures that only validated changes reach the main branch.

Troubleshooting Common Issues with Merge Queues

While GitHub merge queues streamline the merging process, you might still encounter issues. Knowing how to troubleshoot these problems ensures smooth operations.

Reasons for Pull Request Removal from the Queue

Pull requests can be removed from the merge queue for various reasons. Understanding these reasons helps you take corrective actions swiftly:

  • Failed CI Checks: If the continuous integration (CI) checks fail, the pull request is automatically removed. This prevents unstable code from merging into the main branch.

  • Merge Conflicts: A pull request with merge conflicts is taken out of the queue. Conflicts must be resolved before re-adding the pull request.

  • Branch Protection Violations: If a pull request violates branch protection rules, it's removed. These rules might include code review requirements or status checks.

  • User Actions: Users with the right permissions can manually remove a pull request from the queue for various reasons, such as needing further changes.

Handling CI Service Reporting Failures

CI service failures can disrupt the merge queue. Effective handling of these failures is crucial:

  • Check CI Configuration: Ensure the CI configuration triggers on the merge_group event. Missing this event can cause failures in reporting status checks.

  • Review Logs: Examine CI logs to identify the root cause of the failure. Logs provide detailed insights into what went wrong during the build or test process.

  • Retry Mechanisms: Implement retry mechanisms for CI jobs. Sometimes, transient issues can cause failures, and a simple retry might resolve them.

  • Update Dependencies: Outdated dependencies can cause CI failures. Regularly update dependencies to avoid compatibility issues.

Managing Branch Protection Failures

Branch protection rules ensure code quality but might cause pull requests to be removed from the queue:

  • Update Branch Protection Rules: Ensure branch protection rules align with your CI configuration. Inconsistencies can lead to failures.

  • Check Required Status Checks: Verify that all required status checks are defined and correctly configured. Missing status checks can cause the merge queue to halt.

  • Adjust Review Requirements: If your branch protection rules require multiple reviews, ensure that all required reviews are completed before adding the pull request to the queue.

User-Initiated Removals and Timeouts

Users and timeouts can also cause pull requests to exit the merge queue:

  • User Removals: Users with appropriate permissions can remove pull requests manually. This might happen if further changes are needed or if the pull request is no longer relevant.

  • Timeouts: Merge queues have timeout settings defining how long to wait for successful CI checks. If the checks do not report within the specified time, the pull request is removed. Adjusting timeout settings can help manage expectations and reduce unnecessary removals.

By understanding these common issues and their solutions, you can keep your GitHub merge queue running smoothly. Troubleshooting effectively minimizes disruptions, ensuring a more efficient merging process.

Try it yourself or
request a demo

Get started for free

Try it yourself or
Request a Demo

Free for first 5 users