Skip to content

Instantly share code, notes, and snippets.

@danieluhl
Last active July 20, 2023 09:58
Show Gist options
  • Save danieluhl/91ca1b75d4b51864df63223948432402 to your computer and use it in GitHub Desktop.
Save danieluhl/91ca1b75d4b51864df63223948432402 to your computer and use it in GitHub Desktop.
cherry-pick > rebase for stacked patches

https://timothya.com/blog/git-stack/

Generic script that works when there are no conflicts

#!/usr/bin/env bash

git checkout --detach master

BOUNDARY="origin/master"
PUSH=""

while read line; do
    git cherry-pick "$BOUNDARY".."$line"
    git branch --force "$line"
    BOUNDARY="origin/$line"
    PUSH="$PUSH $line:refs/heads/$line"
done

git push -f origin "$PUSH"

Manual process

Use when there might be conflicts between cherry-picks to resolve

git checkout --detach main
git cherry-pick origin/main..f1
git branch --force f1
git cherry-pick origin/main..f2
git branch --force f2
git cherry-pick origin/main..f3
git branch --force f3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment