Git merge main branch into another branch

Greg Foster
Greg Foster
Graphite software engineer

Note: 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
  • 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.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2