git-branch - git move commit to existing branch - Move the most recent commit(s) to a new branch with Git

git cherry-pick multiple commits / git / branching-and-merging

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 

itzmebibin



Answer #1

Yet another way to do this, using just 2 commands. Also keeps your current working tree intact.

git checkout -b newbranch # switch to a new branch
git branch -f master HEAD~3 # make master point to some older commit
git checkout -b newbranch # switch to a new branch
git push . +HEAD~3:master # make master point to some older commit