SZanlongo
4/5/2016 - 11:19 PM

Move the most recent commit(s) to a new branch with Git From: https://stackoverflow.com/questions/1628563/move-the-most-recent-commits-to-a

Moving to a new branch

Unless there are other circumstances involved, 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

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

*1 You will only be "losing" commits from the master branch, but don't worry, you'll have those commits in newbranch!

Moving to an existing branch

WARNING The method above works because you are creating a new branch with the first command: git branch newbranch. If you want to use an existing branch you need to merge your changes into the existing branch before executing git reset --hard HEAD~3. If you don't merge your changes first, they will be lost. So, if you are working with an existing branch it will look like this:

git checkout existingbranch
git merge master
git checkout master
git reset --hard HEAD~3 # Go back 3 commits. You *will* lose uncommitted work.
git checkout existingbranch