1

Based on the git documentation, if I want to save my changes to a new branch the standard produce is

1) git branch new_branch (assuming the branch hasn't been created)
2) git checkout new_branch
3) write new code
4) commit, and it automatically gets put in the new branch

But what if I did things in this order

1) write new code
2) git branch new_branch
3) ???

How do I now save that new code to the new branch?

appleLover
  • 14,835
  • 9
  • 33
  • 50

2 Answers2

2

The same.

You can do the checkout afterwards (as longs as it is a new branch).

3) git checkout new_branch
4) commit, and it automatically gets put in the new branch

Note that you can also create and checkout in one command:

git checkout -b new_branch
blackbuild
  • 5,026
  • 1
  • 23
  • 35
  • what is the checkout command doing here? if i do checkout branch master, git doesn't just change my settings to be in the master branch, it changes the actual files im using to be the files from the master branch... if i create a new branch, it copies the files from the master branch. if i then checkout to the new branch, wouldn't it change the files im using to be the files from the new branch, which were copied from the master branch? – appleLover Apr 10 '14 at 17:02
  • @appleLover Did you _try_ what he said? It seems to me that you have a lot of false beliefs about how git works that are getting in the way here. Listen to what you're being told. – matt Apr 10 '14 at 17:58
  • matt, i did it and it worked. that doesnt change my question. – appleLover Apr 10 '14 at 18:03
  • @appleLover If you create a branch from the actual branch, both branches are identical, i.e. the workspace will not be changed, nor will your edited files be changes. The only thing that changes is the pointer to the current branch, which now points to the new branch, thus the next commit will go into new_branch. – blackbuild Apr 10 '14 at 19:46
0

Commit your changes to your current branch. Then create a new branch from your current branch.

$ git checkout -b new_branch currentbranch

Checkout back your currentbranch

$ git checkout currentbranch

Reset the currentbrach by 1 commit

$ git reset HEAD~1
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
kpopovbg
  • 301
  • 1
  • 6