Last Updated: February 25, 2016
·
1.32K
· joshbuhler

Move git commits to a new branch

Made the mistake of doing a series of commits on our develop branch, and then remembered to create a new feature branch. I wanted to move the commits I had made from the develop branch and into the feature one until they were ready to be merged. This did the trick:

git branch feature-branch develop

This creates a new branch called feature-branch that is essentially a copy of the develop branch I had been working in.

git reset --hard develop HEAD~x

This one rolls back the branch (in my case, develop) the number of commits specified by x. Warning: You will lose uncommitted changes if you do this, so make sure you stash or otherwise save those changes.

Switch to your newly created feature branch, and you're good to go.