Creating git branch after many commits

13,463

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
Share:
13,463

Related videos on Youtube

sunnet
Author by

sunnet

Updated on October 26, 2020

Comments

  • sunnet
    sunnet over 3 years

    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?

  • hobbs
    hobbs about 13 years
    And maybe a git branch --set-upstream newbranch origin/newbranch to set up tracking :)
  • bobDevil
    bobDevil about 13 years
    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.
  • Emmanuel Valle
    Emmanuel Valle about 13 years
    Also, if you want to restore the master branch to the last github push, you should set the <commit_hash> value to HEAD in git reset --hard <commit_hash>: git reset --hard HEAD will do it.
  • sunnet
    sunnet about 13 years
    @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.
  • Chris Johnsen
    Chris Johnsen about 13 years
    @Herbert: First, you probably want to use origin/master (or an equivalent name for the same object) as <commit_hash> 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.
  • sunnet
    sunnet about 13 years
    @Chris -- Thanks, that was helpful, I'll grok this stuff yet.