Sait
Sait

Reputation: 19795

Git branch does not work as expected

I am reading this tutorial: http://gitref.org/branching/

As far as I understand, this tutorials says that if I am in Branch A and make some changes, it is not going to affect other branches.

So I am testing this:

$ mkdir test
$ cd test/
$ git init
Initialized empty Git repository in /root/test/.git/
$ echo 'foo' > foo.txt
$ ls
foo.txt
$ git add *
$ git commit -m 'added foo.txt'
[master (root-commit) 25f6eb7] added foo.txt
1 file changed, 1 insertion(+)
create mode 100644 foo.txt
$ git branch testing # created a new branch
$ git branch
* master
  testing
# I am still in 'master' branch
$ echo 'bar' > bar.txt  # Add a file to 'master' branch
$ ls
bar.txt  foo.txt # makes sense, because we are in 'master'
$ git checkout testing # Let's go to 'testing' branch
Switched to branch 'testing'
$ git branch
  master
* testing
# OK, we are in 'testing'
$ ls
bar.txt  foo.txt # WHY??

So, I changed 'master' branch and it affected 'testing'. What is the problem here?

PS: My git version 1.7.10.4

Upvotes: 1

Views: 153

Answers (2)

zerkms
zerkms

Reputation: 254886

Add a file to 'master' branch --- this is the step when your assumptions start being wrong.

By creating a file with echo 'bar' > bar.txt command you DO NOT add it to a branch.

You should stage it and commit to do so.

So if you:

git add bar.txt
git commit -m "Added a new file"

before checking out to testing then it will work as you expect.

Upvotes: 3

ThiefMaster
ThiefMaster

Reputation: 318468

Untracked (uncommitted) files are not touched when you switch branches. So anything you did after your commit is not affected by branch changes.

Upvotes: 4

Related Questions