Changing the Base Branch for Merging in Git

6 min read.
Tags: git

Sometimes you started a branch from the wrong base and now your merge request includes unrelated commits. You can fix this by moving your branch to the correct base.

The clean way is git rebase --onto.

When to use this

Use this when:

  • feature branch was created from wrong branch
  • target merge branch changed
  • PR/MR diff shows unrelated commits

Command pattern

git rebase --onto <newBase> <oldBase> <featureBranch>

Meaning:

  • take commits in featureBranch that are after oldBase
  • replay them on top of newBase

Real example

You created feature/login-ui from develop, but now should target main:

git checkout feature/login-ui
git rebase --onto main develop feature/login-ui

After this, the branch history is based on main.

Safety checks before rebasing

Run these first:

git status
git log --oneline --decorate --graph -20

If you have uncommitted work, stash or commit it first.

Optional backup branch (recommended):

git branch backup/feature-login-ui

Handle conflicts

If conflict appears:

  1. fix files manually
  2. stage changes: git add <files>
  3. continue: git rebase --continue

If needed, abort:

git rebase --abort

Push after rebase

Because history changed, force push safely:

git push --force-with-lease origin feature/login-ui

Use --force-with-lease, not plain --force.

Related Posts
Latest Posts