Skip to content

Instantly share code, notes, and snippets.

@zoxon
Last active March 24, 2017 09:49
Show Gist options
  • Save zoxon/2f4d2e3a0e26733b5ea6cdf0fc001005 to your computer and use it in GitHub Desktop.
Save zoxon/2f4d2e3a0e26733b5ea6cdf0fc001005 to your computer and use it in GitHub Desktop.

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

http://stackoverflow.com/questions/1628563/move-the-most-recent-commits-to-a-new-branch-with-git

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