Skip to content

Instantly share code, notes, and snippets.

@alesya-h
Last active January 19, 2023 16:39
Show Gist options
  • Save alesya-h/19aa8ce6f1a4ea938a070f403360dcae to your computer and use it in GitHub Desktop.
Save alesya-h/19aa8ce6f1a4ea938a070f403360dcae to your computer and use it in GitHub Desktop.
Turn a fork that has periodic merges of upstream into easily upstreamable rebaseable linear history

Turn a fork that has periodic merges of upstream into easily upstreamable rebaseable linear history (git-imerge rebase through merges)

find commits

git log origin/main..my_fork_with_periodic_merges

mark commits

  1. mark latest common commit before any merges and mark as root point (Ar), its first commit mark A1,A2... upto next merge commit
  2. mark merge point origins (Bo, Co, ...)
  3. mark merge point commits (Bm, Cm, ...) and commits after them B1,B2, ..., C1, C2, ...
  4. create branches A,B,C, ... from last commit before the merge
Ar -> T            # parent of A1    
A1                 # oldest commit that is returned by the command above    
A2                 # A1's child    
A3                 # A2's child    
Bm (merge A3, Bo)  # merge-commit in the fork that merges origin/main    
B1                 # Bm's child    
B2                 # B1's child    
Cm (merge B2, Co)  # and so on...    
C1    
C2    
C3    
C4

do the magic

git switch Ar -C T

git cherry-pick A1,A2,A3
git imerge rebase Bo
git imerge finish
git switch -C A-over-Bo-main
git switch Bm -C A-over-Bo-full
git reset --soft A-over-Bo-main
git commit -m "Additional changes from Bm: $(extract-commit-message Bm)"
git checkout T
git reset --hard A-over-Bo-full

git cherry-pick B1, B2
git imerge rebase Co
git imerge finish
git switch -C B-over-Co-main
git switch Cm -C B-over-Co-full
git reset --soft B-over-Co-main
git commit -m "Additional changes from Cm: $(extract-commit-message Cm)"
git checkout T
git reset --hard B-over-Co-full

git cherry-pick C1, C2, C3, C4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment