1

I've created some changes to a git repo, and committed them to master (I have NOT pushed the changes up to GitHub, however). What I need to do now, is create a new branch, and move my commits over to this new branch. The changes are quite large and so manually redoing the changes on a new branch is not possible - hopefully there is some set of commands which will rewind master to before my commits, move my commits onto a new branch, and then push these up.

I did search for other questions but I didn't see any that exactly fitted my situation, so wanted to get an exact answer.

Thanks in advance!

Javawag
  • 1,547
  • 2
  • 16
  • 23

2 Answers2

3

That's fairly easy:

# make sure you're on master
git checkout master
# create a new branch that is identical to master
git branch mystuff master
# reset your local master branch to the state of the remote master
git reset --hard origin/master
# push your new branch to the remote
git push origin mystuff
Michael Wild
  • 24,977
  • 3
  • 43
  • 43
  • This worked flawlessly - Fernaref's may have also worked but since this seemed easier I tried this first :P – Javawag Mar 27 '13 at 13:51
  • Yea, it is easier when the commit you want to roll back to is the current HEAD of origin/master. – Femaref Mar 27 '13 at 21:19
1

First, create the new branch, then rewind master:

git branch new_branch
git reset --hard <sha1-id>

To find the needed sha1-id, check git log.

Afterwards, you can push that branch up to your remote:

git checkout new_branch
git push -u origin new_branch

Note that -u will set up a tracking branch, meaning you will be able to issue git pull and git push without specifing origin new_branch from now on.

Femaref
  • 60,705
  • 7
  • 138
  • 176