Git rebase accept all incoming changes

Greg Foster
Greg Foster
Graphite software engineer

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).

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.

  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 main.

  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.

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

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