12

I have been working on a branch which we can call "A". I just realized that the code I have added since I last committed should rather be in a specific (experimental) branch, but not in "A". How can I commit the changes to a new branch and leave branch "A" as it was when I last committed?

essential
  • 648
  • 1
  • 7
  • 19
Siggi
  • 319
  • 3
  • 11

2 Answers2

23

If the changes are staged or your working directory, you can simply checkout into a new branch like so:

git checkout -b branch_name

You can then commit directly into the new branch.

Blair Holloway
  • 15,969
  • 2
  • 29
  • 28
  • Sometimes things are just too simple. Thanks mate. – Siggi Jul 12 '10 at 12:33
  • @Siggi: You might want to accept this answer so others with the same question can find it more easily (and to give credit to Blair). – Cascabel Jul 12 '10 at 17:13
  • Done, I just couldn't do it when he posted the answer because of the "accept answer" time limit... and then I forgot. – Siggi Jul 17 '10 at 13:12
5
git stash
git checkout branch-A
git stash pop
Verhogen
  • 27,221
  • 34
  • 90
  • 109