Graphite Reviewer is now Diamond

Git merge main branch into another branch

Greg Foster
Greg Foster
Graphite software engineer
Try Graphite


Note

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


While it's possible to sync a feature branch with the main branch by merging main into the feature, using a rebase instead maintains a cleaner project history. Merging main into a feature branch introduces a merge commit each time, which can clutter the branch's history with numerous small merges that might complicate the timeline and make troubleshooting more difficult. On the other hand, rebasing the feature branch onto main rewrites the feature branch’s history to appear as if it had started from the latest commit on main, creating a linear and much cleaner history.

This not only simplifies future merges but also makes it easier to understand the flow of changes when looking at the project history. Additionally, rebasing helps in ensuring that testing the feature branch is more effective since it reflects what the final merge result will look like, reducing the chances of post-merge issues in the main branch.

That being said, this guide will walk you through the process of merging changes from the main branch into another branch in Git.

First, let's clarify some key concepts related to merging in Git:

  • Merge: The process of taking the changes from one branch and integrating them into another.
  • Main branch: The main branch in Git is the default development branch where the stable codebase is maintained and all changes are eventually merged.
  • Feature branch: Branches where development of new features or experiments are done. These branches diverge from the main branch and are later merged back.

Merging the main branch into a feature branch is typically done for a few reasons:

  • To ensure the feature branch has the latest updates from the main branch, which can include critical bug fixes, security updates, or other important changes.
  • To reduce the complexity of a future merge back into the main branch by regularly updating the feature branch.

Before merging, it's important to ensure that your local repository is up to date. Here are the steps you would typically follow:

  1. Switch to your feature branch: Ensure you are on the branch into which you want to merge changes.

    Terminal
    git checkout feature-branch

    Replace feature-branch with the name of your branch.

  2. Fetch the latest changes from the remote repository: This step updates your tracking branches (including origin/main).

    Terminal
    git fetch origin

    This command retrieves the latest commits and data from the specified remote repository, in this case named "origin", without integrating any of these changes into your local branches.

    It updates your local tracking branches, such as origin/main, to reflect the current state of these branches on the remote, enabling you to see any new commits made by others.

  3. Merge the main branch into your feature branch: Now that your local repository is up to date, you can merge the main branch into your feature branch.

    Terminal
    git merge origin/main

    This command brings in the changes from origin/main to your current branch (feature-branch).

Conflicts may arise during the merge if the changes in the main branch are in conflict with the changes in your feature branch. Here’s how to handle them:

  1. Identify the files with conflicts: Git will list conflicts during the merge. You can also find them with:

    Terminal
    git status
  2. Resolve the conflicts: Open the conflicting files and resolve the merge conflicts by choosing which versions of the code you wish to accept. See this guide on resolving merge conflicts for a more detailed walkthrough.

  3. Add the resolved files to the staging area: After you resolve the conflicts in a file, add it to the staging area.

    Terminal
    git add <filename>
  4. Complete the merge: Once all conflicts are resolved and added, you can complete the merge.

    Terminal
    git commit

    This will open an editor to enter a commit message for the merge. Save and close the editor to complete the commit.

After merging and resolving any conflicts, push your changes to the remote repository to share them with your team.

Terminal
git push origin feature-branch

You can achieve the same result—rebasing a feature branch onto the latest main—using the Graphite CLI. Graphite simplifies this process and offers additional tools to manage stacked branches effectively.

To rebase your feature branch onto the latest main using Graphite, follow these steps:

  1. Sync your local repository with the latest changes from main:

    Terminal
    gt sync

    This command pulls the latest changes into main and restacks (rebases) your open pull requests on top of these updates.

  2. Resolve any merge conflicts:

    If conflicts arise during the rebase, Graphite will prompt you to resolve them. After resolving, stage the changes and continue the rebase:

    Terminal
    git add <resolved-files>
    gt continue

    This process ensures that your feature branch is cleanly rebased onto the latest main, maintaining a linear project history.

Graphite provides several commands to manage and update your branches effectively:

  • gt move: Rebase the current branch onto a target branch and restack all its descendants. This is useful when you want to change the base of your feature branch.

  • gt restack: Ensure each branch in the current stack has its parent in its Git commit history, rebasing if necessary. This is particularly helpful after making changes to a parent branch.

  • gt modify: Amend the last commit in the current branch and automatically restack all branches above it. This is ideal for incorporating feedback or making minor adjustments.

By leveraging these commands, you can maintain a clean and organized commit history, simplifying code reviews and collaboration.

  • Regularly merge the main branch: Regularly merging the main branch into your feature branches can help minimize merge conflicts.
  • Keep your feature branches focused and short-lived: Smaller, more focused branches tend to have fewer conflicts and are easier to manage.

For further reading on merging branches in Git, see the official Git documentation.

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