46

I have a repo on GitHub with a single branch master that I've been working on in my local repo. At some point I stopped pushing commits back up to master on GitHub because I was worried that they'd break things. Now I have lots of commits in my local repo that I want to push back up to GitHub.

However, rather than pushing back up to master I would prefer to create a new branch on GitHub (development) and push all my local commits back up to that branch (which I will merge back into master only after they've been better tested).

What is the simple way to do this?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Herbert Sitz
  • 21,858
  • 9
  • 50
  • 54
  • 2
    possible duplicate of [How can I move recent commit(s) to a new branch with git?](http://stackoverflow.com/questions/1628563/how-can-i-move-recent-commits-to-a-new-branch-with-git) – tripleee Jun 03 '14 at 05:25

1 Answers1

67

on master:

git checkout -b newbranch or as suggested below, simply do a git branch newbranch to create the new branch without switching to it.

This will create a branch that is at your current commit at master. Once done:

git checkout master

followed by:

git reset --hard <commit_hash>

Where <commit_hash> should be replaced by the commit ID you want master rolled back to.

Now you can switch to your new branch and push it out to the remote.

git checkout newbranch
git push origin newbranch
Grant Limberg
  • 20,913
  • 11
  • 63
  • 84
  • 2
    And maybe a `git branch --set-upstream newbranch origin/newbranch` to set up tracking :) – hobbs Mar 17 '11 at 18:22
  • 1
    If you just do 'git branch newbranch', instead of 'git checkout -b newbranch; git checkout master' then the branch will get created pointing to the proper commit, without actually changing branches. – bobDevil Mar 17 '11 at 18:22
  • 1
    Also, if you want to restore the master branch to the last github push, you should set the value to HEAD in git reset --hard : git reset --hard HEAD will do it. – Emmanuel Valle Mar 17 '11 at 18:24
  • @Grant -- Thanks very much, that did the trick! Makes sense to me now, but it would have taken me a while to get there myself. One question: If I simply wanted my local Master to be same as github Master could I have used `git checkout origin/master` (or Emmanuel suggestion in comment above) rather than the hard reset to the commit-hash? If so, would I have had to delete my local Master first (and would I have done that by switching to my new development branch and issuing `git branch -d master`)? Thanks again. – Herbert Sitz Mar 17 '11 at 19:43
  • 4
    @Herbert: First, you probably want to use `origin/master` (or an equivalent name for the same object) as `` in Grant’s command. Second, if you want to move your local `master` to wherever `origin/master` points, then it is best to do it directly (since deleting and recreating it would also restart its reflog). If `master` is checked out, then `git reset --hard origin/master`. If it is not checked out, then `git branch -f master origin/master`. Your `git checkout origin/master` would check out the commit as a detached HEAD, which is probably not what you want. – Chris Johnsen Mar 18 '11 at 05:33
  • @Chris -- Thanks, that was helpful, I'll grok this stuff yet. – Herbert Sitz Mar 18 '11 at 19:38