views:

1553

answers:

1

I'd like to move the last several commits I've made to master to a new branch and take master back to before those commits were made. Unfortunately, my Git-fu isn't strong enough yet, any help?

Thanks.

+25  A: 

Unless there are other circumstances involved, this can be easily done by branching and rolling back.

git branch my-new-fangled-branch
git reset --hard HEAD~3
git checkout my-new-fangled-branch

But do make sure how many commits to go back.

sykora
And in particular, *don't* try to go back further than the point where you last pushed commits to another repository from which somebody else might have pulled.
Greg Hewgill
Perfect, thanks.
Mark A. Nicolosi
Wondering if you can explain WHY this works.To me you're creating a new branch, removing 3 commits from the old branch you are still on, and then checking out the branch you made.So how do the commits you removed magically show up in the new branch?
Jonathan Dumaine
@Jonathan Dumaine: Because I created the new branch before removing the commits from the old branch. They're still there in the new branch.
sykora
I see. Branching makes of clone of the state it is currently in. For some reason I was under the assumption that those commits would not get cloned because they were not on a remote server (too much git-svn haha). Thank you.
Jonathan Dumaine
branches in git are just markers which point to commits in history, there is nothing being cloned, created or deleted (except the markers)
knittl