Question

I created a new repo:

git init

Created a new file and added text to it.

touch hello.txt

Created a new branch:

git branch -b halo

Created a new file and added text to it

touch halo.txt

Now technically this file should not be shown in the branch master. Even if I track this in the new branch, it shows tracked in master. I haven't used git branches in last 3-4 months and I am wondering if something has changed OR if this was always the case?

Was it helpful?

Solution

If you did not commit files to a branch, they are not in the branch. If you just did git add, then you staged those files. They are tracked by branches in any way. If you switch branches with some files staged, they will still be staged. It always worked this way as long as I can remember.

ORIGINAL ANSWER (for reference)

You must be doing something wrong. You didn't share all your commands, most notably the commits and how you display the content of branches. But consider this:

$ git init /tmp/test1
$ cd /tmp/test1
$ touch hello.txt
$ git add .
$ git commit -m added
[master (root-commit) 12778a0] added
 0 files changed
 create mode 100644 hello.txt
$ git checkout -b halo
Switched to a new branch 'halo'
$ touch halo.txt
$ git add halo.txt
$ git commit -m 'added halo'
[halo 9a2b380] added halo
 0 files changed
 create mode 100644 halo.txt
$ git checkout master
Switched to branch 'master'
$ git ls-files
hello.txt
$ git ls-files --with-tree halo
halo.txt
hello.txt

Everything works as intended. This is in Git 1.7.10.4

There was one mistake in your commands:

git branch -b halo

At least in my version git branch doesn't have a -b option. I assume you meant git checkout -b halo so that's what I used above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top