Move the Most Recent Commit(S) to a New Branch with Git
I'd Like to Move the Last Several Commits I've Committed to Master to a New Branch and Take Master Back to Before Those Commits Were Made. Unfortunately, My...
I'd like to move the last several commits I've committed to master to a new branch and take master back to before those commits were made. Unfortunately, my Git-fu is not strong enough yet, any help?
I.e. How can I go from this
master A - B - C - D - E
to this?
newbranch C - D - E
/
master A - B
21 Answers
Must Read
Moving to an existing branch
If you want to move your commits to 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
You can store uncommitted edits to your stash before doing this, using git stash. Once complete, you can retrieve the stashed uncommitted edits with git stash pop