Skip to content

Instantly share code, notes, and snippets.

@SteGriff
Created October 8, 2021 13:08
Show Gist options
  • Save SteGriff/fe6cd0da85545a25e4a800ce732a0762 to your computer and use it in GitHub Desktop.
Save SteGriff/fe6cd0da85545a25e4a800ce732a0762 to your computer and use it in GitHub Desktop.
Git move commits to another branch
# == Check how many commits to move ==
git log
# == Move commits to a new branch ==
# Creates the branch at current HEAD but doesn't check it out:
git branch new-branch
# Resets master/main/current back x commits
git reset --keep HEAD~2
# Go to the new branch which has the new commits in it
git checkout new-branch
# Push upstream
git push -u origin new-branch
# == Move commits to an existing branch ==
# Check out the branch and get the stuff from master into it
git checkout existing-branch
git merge master
# Go back to master and set it straight
git checkout master
git reset --keep HEAD~2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment