Skip to content

Instantly share code, notes, and snippets.

@holmberd
Last active April 9, 2024 02:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holmberd/4ed2f1a93604c75cc4d297b0b65ec802 to your computer and use it in GitHub Desktop.
Save holmberd/4ed2f1a93604c75cc4d297b0b65ec802 to your computer and use it in GitHub Desktop.
Git Rebase --onto

Linked Feature Branches

Consider a case where you have multiple features that all depending on each-other going into master at different times. Feature-C depends on Feature-B, and Feature-B depends on Feature-A and so on...

If we squash merge Feature-A into master we first have to resolve our base in Feature-B before it can be merged. Below is an example of resolving it after Feature-A has been merged into master (make sure master is up-to-date).

git fetch --all
git rebase --onto master Feature-A Feature-B
git push -f

To resolve Feature-C we need the old-base(Feature-B) that C is depending on; which is now rewritten since we forced-pushed the changes to origin. But the base exist in the commit logs for Feature-C, and we can create a new base from the commit hash in C's logs where we first started basing of Feature-B.

Again with the new base created we can now do:

git rebase --onto master old-base Feature-C
git push -f

Master Changed Conflicts

In sitations where there are open PRs and origin/master history has been modifed and forced-pushed.

  • Find the last commit in the old master before the first commit of your branch (copy), e.g. git rebase --onto origin/main $(git merge-base main branch-your-branch-depend-on) your-branch

  • git checkout master && git pull

  • git checkout your-branch

  • git rebase --onto master last-commit-before-first-pr-commit (rebase every commit after last-commit and put it onto master).

  • Resolve any conflicts.

  • git push -f

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