4

I would like to know if and how I can create a new branch when I had already committed but not pushed.

Scenario:

  1. Switch to Master
  2. Change something
  3. Commit changes
  4. Create new Branch instead of Push

Basically I was told that this is not possible and that one has to push before creating a new branch. Since I did two days of work (like 10+ commits) and came to the conclusion that I would rather create a new branch instead of pushing these changes to master (I want to do additional testing prior since the changes became more bigger than anticipated).

Would I need to save all mal files, recheckout the master version I started with, create a new branch from it and restore my changes and recommit or can I just create a new branch which I originally thought (so I did not care at first)?

Martin Kersten
  • 5,127
  • 8
  • 46
  • 77

4 Answers4

4

Basically I was told that this is not possible and that one has to push before creating a new branch.

That is absolutely not the case. You can create a new branch whenever you want. For example, after you had committed your changes, you could run:

git checkout -b newbranch

And that would create a new branch named newbranch based on the current commit. If you want to revert the master branch as part of this operation (that is, you want your changes only on the new branch, not on the master branch) you could do this:

# create a new branch, but don't switch to it
git branch newbranch

# Reset current (master) branch by discarding the most recent
# commit.
git reset --hard HEAD^

# Switch to new branch
git checkout newbranch

And now you can push your new branch to the remote server.

larsks
  • 277,717
  • 41
  • 399
  • 399
1

Create a branch with your latest commits:

git branch newbranch

Reset your master branch to the server state:

git reset --hard origin/master

Check out the newbranch:

git checkout newbranch

See more

Community
  • 1
  • 1
filhit
  • 2,084
  • 1
  • 21
  • 34
0

You can just do a git branch new_branchname. The default starting position is HEAD. It can be changed if you wish by adding it as a second parameter to git branch.

What you do have now is that the new commit is still present on master, which seems to me is not the final intention. You'll need to revert that or even reset (because it hasn't been pushed this is fine).

rubenvb
  • 74,642
  • 33
  • 187
  • 332
0
$ git checkout dde0f681c20791aee8dcc0f31b41850f83c5b778 (id commit )


$ git checkout -b before_bo 

$ git push --set-upstream origin before_bo

$ git checkout master
zloctb
  • 10,592
  • 8
  • 70
  • 89