1

Let's suppose I am on master branch and then I do :

git branch new_branch
git checkout new_branch
touch newFile.txt

When I do git checkout master to come back to my master branch, I expect I have a clean workspace and no trace of newFile.txt. But git automatically moves newFile.txt in my master branch.

Is this the default git behavior? How could I change it?

Thanks.

rico
  • 1,781
  • 1
  • 21
  • 36
  • Yes, but where do you want those changes to go? Would you rather have git silently remove all changed files? – Edward Thomson Sep 24 '14 at 11:23
  • No, I suppose what I do in new_branch should stay in new_branch. If I start to do something on a branch, and then I have an emergency I have to switch to another one, I don't want to get the work done on the other branch. – rico Sep 24 '14 at 11:24
  • @rico: you are not doing anything in new_branch, you are doing something in your working tree. And the working tree is not linked to any branch. – knittl Sep 24 '14 at 11:26
  • Your changes aren't on any branch until you commit them; for this workflow, you need to either stash or commit your changes then. – Edward Thomson Sep 24 '14 at 11:26

1 Answers1

3

Uncommitted changes are not part of any branch, they live in the working copy. To remove untracked files, use the git clean command. This is default behavior of Git and cannot be changed.

Also, see Move existing, uncommited work to a new branch in Git. Whether or not the branch is new or an existing does not really matter.

Community
  • 1
  • 1
knittl
  • 197,664
  • 43
  • 269
  • 318