Creating git branch after many commits
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
Related videos on Youtube
sunnet
Updated on October 26, 2020Comments
-
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?
-
tripleee almost 10 yearspossible duplicate of How can I move recent commit(s) to a new branch with git?
-
-
hobbs about 13 yearsAnd maybe a
git branch --set-upstream newbranch origin/newbranch
to set up tracking :) -
bobDevil about 13 yearsIf 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 about 13 yearsAlso, 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 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 issuinggit branch -d master
)? Thanks again. -
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 localmaster
to whereverorigin/master
points, then it is best to do it directly (since deleting and recreating it would also restart its reflog). Ifmaster
is checked out, thengit reset --hard origin/master
. If it is not checked out, thengit branch -f master origin/master
. Yourgit checkout origin/master
would check out the commit as a detached HEAD, which is probably not what you want. -
sunnet about 13 years@Chris -- Thanks, that was helpful, I'll grok this stuff yet.