Gregg
Gregg

Reputation: 35904

Working in Git Branch, and then deciding a need a new branch, some files already committed

I know that if I'm working away in a branch I can create a new branch and all my commits will go to the new one. But what if I'm working along, I've made some commits, still working, and then realize a need a new branch, including the commits I've already made (but not pushed to origin). Is there a way I can get everything into a new branch?

Upvotes: 1

Views: 45

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 994361

Sure, do the following:

git checkout -b new_branch

This creates a new branch from the current HEAD. Then:

git stash

This stores the current changes away for the moment. Then:

git checkout former_branch

Go back to the former branch, and:

git reset --hard HEAD^

This rewinds one commit off the end of former_branch (use whatever you like to point to the commit you want to go back to, maybe origin/master or something). Finally:

git checkout new_branch
git stash pop

and carry on working.

Upvotes: 3

Related Questions