3

I'm on local branch A and did some changes without committing (they are not done). Then I want to create a new branch B (to work on something different) without bringing in the changes in branch A. If I do 'git checkout -b B', the changes in branch A will be brought to the new branch B. How can I avoid this? I want a brand new branch B without the changes in branch A, and I don't want to commit the changes in A at this moment because they are not finished.

Thanks guys!

garthcn
  • 229
  • 1
  • 4
  • 11

3 Answers3

4

Use git stash to store your changes in A away for the moment. Later on you can get them back with git stash pop.

You can list the stashes you made with git stash list. Also handy is viewing what changes are in a stash with git stash show -p ...

Benjamin Bannier
  • 55,163
  • 11
  • 60
  • 80
3

You can stash your changes before checking out a new branch.

git stash
git checkout -b B

If you have some new untracked files and you want them gone too, you can first add them (but not commit):

git add .
git stash
git checkout -b B

Later you will be able to untrack them by

git reset HEAD file
dmedvinsky
  • 8,046
  • 3
  • 31
  • 25
3
$ git branch
* a
  b
$ git stash
$ git checkout b
$ vim file0 file1
$ git commit -a -m'k done'
$ git checkout a
$ git stash pop
wilhelmtell
  • 57,473
  • 20
  • 96
  • 131