Skip to content

Instantly share code, notes, and snippets.

@gioragutt
Last active July 25, 2025 16:51
Show Gist options
  • Save gioragutt/b670371f2c6ce8e391c1e87655130300 to your computer and use it in GitHub Desktop.
Save gioragutt/b670371f2c6ce8e391c1e87655130300 to your computer and use it in GitHub Desktop.
How do deal with rebasing multiple branches on top of one another

I had several branches in a stack:

* feature-part-4 (HEAD)
* feature-part-3
* feature-part-2
* feature-part-1
* main

Wanted to insert a change into feature-part-1, so what I did was:

git rebase -i main --update-refs

There I changed feature-part-1's command to e/edit instead of p/pick, fixed it, and continued the rebase. The conflicts down the rebase were handled cleanly and it worked right out of the box.

12/10 would recommend 👌🏻

Rebasing a series of branches in Git, particularly when they are stacked or dependent on each other, can be achieved efficiently using the git rebase --update-refs option (available from Git 2.38 onwards) or by manually rebasing each branch in sequence.

Using git rebase --update-refs for Stacked Branches (Recommended for Git 2.38+)

Ensure your base branch is up-to-date:

git fetch origin main:main # Or your equivalent base branch

Checkout the topmost (farthest) branch in your stack:

git checkout branch3 # Assuming branch3 depends on branch2, which depends on branch1

Rebase the entire stack.

git rebase main --update-refs

This command will rebase branch3 onto main, and automatically update the references of branch2 and branch1 to follow the rebased branch3.

Manually Rebasing a Series of Branches

If you are using an older Git version or prefer manual control, you can rebase each branch sequentially:

  • Ensure your base branch is up-to-date:
git fetch origin main:main

Rebase the first dependent branch.

git checkout branch1
git rebase main
  • Rebase the next dependent branch onto the rebased previous branch:
git checkout branch2
git rebase branch1
  • Continue this process for all subsequent dependent branches:
git checkout branch3
git rebase branch2
# ... and so on

Important Considerations:

Force Pushing:

If you have pushed any of these branches to a remote repository before rebasing, you will need to force-push them after the rebase, as their history has been rewritten. Use git push origin --force-with-lease to prevent overwriting others' commits.

Conflict Resolution:

During the rebase process, you may encounter merge conflicts. Resolve these conflicts, git add the changes, and then git rebase --continue.

Shared Branches:

Avoid rebasing branches that have been shared with others and on which others are actively working, as it rewrites history and can cause issues for collaborators. Rebasing is best used for local, unshared branches or for cleaning up feature branches before merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment