Read Anthropic’s case study about Graphite Reviewer

How to merge two Git 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 two Git branches allows you to combine the changes from two branches into a new one or one of the existing branches. This is useful for the combination of feature developments and bug fixes into a main line of development, to simplify project updates and minimize code discrepancies. This guide will cover how to merge two branches in various ways, including the use of Graphite's "Merge when ready" feature for optimized branch management.

Merging is a Git operation that integrates changes from one branch into another. This can be done in several ways, depending on your workflow needs:

  • Merging two branches into a new branch
  • Merging two branches into one of the existing branches
  • Handling branches with different commit histories

The basic command to merge two branches in Git is:

Terminal
git merge <branch-name>

Where <branch-name> is the name of the branch you want to merge into the current branch you are checked out on.

Before merging, make sure your local repository is current:

Terminal
git fetch origin

Switch to the branch you want to merge the changes into:

Terminal
git checkout <target-branch>

Now, merge the feature branch into your target branch:

Terminal
git merge <feature-branch>

If there are no conflicts, Git will perform a fast-forward merge. If conflicts arise, you'll need to resolve them manually.

If Git indicates conflicts, you must resolve these before completing the merge:

  • Open the conflicted files and make the necessary changes.
  • Mark the conflicts as resolved by staging the updated files:
Terminal
git add <resolved-file>

Once conflicts are resolved, commit the merge:

Terminal
git commit -m "Merge <feature-branch> into <target-branch>"

After the merge is complete, push your changes to the remote repository:

Terminal
git push origin <target-branch>

If you are merging two branches that do not share a direct lineage, use the --allow-unrelated-histories option:

Terminal
git merge <feature-branch> --allow-unrelated-histories

Graphite offers a "Merge when ready" feature, similar to GitHub's automerge, but with an emphasis on managing PR stacks:

  • Enable Merge when ready: Navigate to the PR in Graphite and toggle the "Merge when ready" switch next to the merge button. This will allow Graphite to automatically merge the PR once all branch protection rules are met.
  • Stacked PRs: For a stack of PRs, enable "Merge when ready" at the top-most PR. Graphite will handle the merging process as each PR in the stack becomes ready.

This feature is particularly useful for maintaining a clean commit history and reducing the manual effort involved in managing pull requests.

By following these steps, you can effectively merge two Git branches, whether into an existing branch or a new one. Leveraging tools like Graphite can further secure your workflow and ensure that branches are merged only when all conditions are met, maintaining the integrity of your project's development history.

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