Skip to content

Instantly share code, notes, and snippets.

@ricardogarfe
Last active October 14, 2019 11:37
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 ricardogarfe/ebb1b5ae336b3735aeb4 to your computer and use it in GitHub Desktop.
Save ricardogarfe/ebb1b5ae336b3735aeb4 to your computer and use it in GitHub Desktop.
Move git commmits to a new branch

Move commmits between branches

From this:

master A - B - C - D - E

Move C - D - E to a new branch:

newbranch     C - D - E
             /
master A - B 

This can be easily done by branching and rolling back.

git branch newbranch
git reset --hard HEAD~3 # Go back 3 commits. You *will* lose uncommitted work.*1
git checkout newbranch
git push origin newbranch

But do make sure how many commits to go back. Alternatively, you can instead of HEAD~3, simply provide the hash of the commit you want to "revert back to" on the master (/current) branch, e.g:

git reset --hard a1b2c3d4

And remove commits from master:

git checkout master
git reset --hard HEAD~3
git push --force

Just rewriting history.

Documentation

Extracted from stackoverflow question Move the most recent commit(s) to a new branch with Git

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