Skip to content

Instantly share code, notes, and snippets.

@iampeterbanjo
Forked from unbracketed/branch-fu.md
Created April 22, 2022 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iampeterbanjo/9d9f174da96f12c1fdd6e65bbb5f8ae4 to your computer and use it in GitHub Desktop.
Save iampeterbanjo/9d9f174da96f12c1fdd6e65bbb5f8ae4 to your computer and use it in GitHub Desktop.
Moving commits between branches

Example: Moving up to a few commits to another branch

Branch A has commits (X,Y) that also need to be in Branch B. The cherry-pick operations should be done in the same chronological order that the commits appear in Branch A.

cherry-pick does support a range of commits, but if you have merge commits in that range, it gets really complicated

git checkout branch-B
git cherry-pick X
git cherry-pick Y

Example: Moving a range of commits

Branch A has a series of commits (X..Y) that need to be moved to branch B. For this case, you'll need to specify the commit before the initial commit you're interested in order for it to be included. Example: you want commits B..D from (...A->B->C->D-E->...) you would use "A" as the starting commit. You'll also need the commit SHA for the HEAD of the branch you are transferring to.

git checkout branch-B
git log  # Note the SHA of most recent commit (M)
git rebase --onto M <commit before X> Y
git rebase HEAD branch-B
@iampeterbanjo
Copy link
Author

I used this today and the Git did not implode 🎉

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