Read Anthropic’s case study about Graphite Reviewer

How to sync a Git branch with the main branch

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

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


This guide provides an in-depth explanation of how to sync a Git branch with the main branch using various Git commands.

Syncing a feature branch with the main branch ensures that your branch has the latest updates, bug fixes, and features. This practice helps prevent merge conflicts, maintain compatibility, and keep your development process streamlined.

Rebasing integrates changes from the main branch into your feature branch by replaying your changes on top of the latest commits from the main branch. This method keeps a linear project history, which is easier to follow.

Steps to fetch and rebase

  1. Fetch the latest changes from the remote repository:

    Terminal
    git fetch origin
  2. Checkout your feature branch:

    Terminal
    git checkout your-feature-branch
  3. Rebase your feature branch onto the main branch:

    Terminal
    git rebase origin/main

    If there are any conflicts, Git will pause the rebase process and prompt you to resolve them. After resolving conflicts, continue the rebase with:

    Terminal
    git rebase --continue
  4. Push the rebased branch to the remote repository:

    Terminal
    git push -f origin your-feature-branch

    The -f flag (force) is necessary because rebasing rewrites commit history, which requires force pushing to update the remote branch.

Merging is another method to sync your feature branch with the main branch. This method retains the history of both branches, including the merge commit.

Steps to merge main into your feature branch

  1. Fetch the latest changes from the remote repository:

    Terminal
    git fetch origin
  2. Checkout your feature branch:

    Terminal
    git checkout your-feature-branch
  3. Merge the main branch into your feature branch:

    Terminal
    git merge origin/main

    If there are any conflicts, Git will prompt you to resolve them. After resolving conflicts, complete the merge with:

    Terminal
    git commit
  4. Push the merged branch to the remote repository:

    Terminal
    git push origin your-feature-branch

If you decide that your feature branch changes are not needed, you can reset your branch to match the main branch. This method discards all local changes.

Steps to reset to main

  1. Fetch the latest changes from the remote repository:

    Terminal
    git fetch origin
  2. Checkout your feature branch:

    Terminal
    git checkout your-feature-branch
  3. Reset your feature branch to match the main branch:

    Terminal
    git reset --hard origin/main
  4. Push the reset branch to the remote repository:

    Terminal
    git push -f origin your-feature-branch

For further reading on keeping your branch in sync with main, see this guide on rebasing in Git.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2