Git rebase accept all incoming changes

Greg Foster
Greg Foster
Graphite software engineer


Note

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


Git rebase is a command that allows developers to integrate changes from one branch into another by moving or combining a sequence of commits to a new base commit. When rebasing, conflicts may arise, but luckily Git provides strategies to resolve these conflicts. One common requirement during a rebase is to accept all incoming changes or, conversely, keep all current changes. This guide will explain how to handle these situations effectively using Git commands.

When you rebase a branch, Git tries to apply each commit from the feature branch onto the target branch. If the changes in the commits conflict with changes in the target branch, Git will pause the rebase and ask you to resolve the conflicts.

  • Incoming changes: Changes that are in the branch you are trying to rebase onto (usually the base branch).
  • Current changes: Changes that are in your working branch (the one being rebased).
Join 20,000+ developers at top companies
Stop wrestling with Git commands
The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop googling Git commands.
main
diff1
diff2

Git offers several strategies to automate conflict resolution during a rebase, allowing you to select either "ours" (current changes) or "theirs" (incoming changes) automatically.

To accept all incoming changes during a rebase, you can use the -X option with the theirs strategy. This option tells Git to resolve all conflicts by preferring changes from the branch you are rebasing onto.

Terminal
git rebase -X theirs target-branch

This command will start the rebase process and automatically resolve any conflicts by accepting the incoming changes from the target-branch.

Conversely, if you want to keep all changes from your current branch and ignore the incoming changes during the rebase, you can use the ours strategy.

Terminal
git rebase -X ours target-branch

This command tells Git to resolve all conflicts by keeping the changes in the current branch that you are rebasing.

While Git is an incredibly useful tool, it has many shortcomings, particularly with rebasing, and managing stacked pull requests.

The Graphite CLI simplifies git, handles rebasing automatically, and allows you to create, submit, and stack pull requests right from the command line.

Under the hood, the CLI runs Git to create branches, commits, and metadata, which means you can still use Git in your scripts, tooling, or whenever you feel like it. Read more about installing the Graphite CLI in our docs.

screenshot of the Graphite CLI

  1. Start the rebase: Begin by checking out the branch that you want to rebase.

    Terminal
    git checkout feature-branch
    git rebase -X theirs main

    Replace main with whatever branch you are rebasing onto.

  2. Monitor the rebase process: Git will attempt to reapply each commit from the feature-branch onto main. If there are conflicts and you've specified -X theirs, Git will automatically favor the changes from feature-branch.

  3. Continue or abort rebase: If you're happy with the automated conflict resolution:

    Terminal
    git rebase --continue

    If something goes wrong or you're not happy with the results, you can abort the rebase:

    Terminal
    git rebase --abort

    This cancels the current rebase operation, reverting the repository to its state before the rebase began. This action undoes any changes made during the rebase process, restoring the original branch and commit history.

  4. Verify and Push Changes: Once the rebase is complete, it's a good idea to review the changes. If everything looks correct, you can push the changes to your remote repository.

    Terminal
    git push --force

    Using --force is necessary because you've rewritten the commit history on your branch.

Join 20,000+ developers at top companies
The best engineers use Graphite to simplify Git
Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.
main
diff1
diff2
  • Backup Before Rebasing: Consider creating a backup branch before starting a rebase, especially if the rebase is complex or involves significant changes.
  • Use a Clean Working Directory: Ensure your working directory is clean (all changes committed) before starting a rebase to avoid losing uncommitted work.
  • Frequent Communication: If you're working in a team and rebasing shared branches, communicate with your team to ensure that everyone is aware of the changes.

Rebasing in Git can simplify your project history and make integrating changes from different branches easier. By using the -X theirs or -X ours options during a rebase, you can automate conflict resolution, either by accepting all incoming changes or keeping all current changes. Understanding these options allows you to maintain control over your codebase and ensure that your project's history is clean and manageable.

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