1

I have been exploring git and I noticed that when I checkout different branches in terminal I notice the changes reflected in finder. So, if branchA has file1 and branchB has file1 as well as file2 and i run git checkout branchA, I look in finder I see file1 in my git directory. But if I run git checkout branchB and look in finder I see file1, file2. Ok, great. It seems git modifies the current directory based on which branch is checked out.

However, I recently cloned a remote repo. Lets say master has file1 in it and that is it. I am in master. I than create newBranch and checkout to it. I run echo "test" > test.txt. Now when I checkout either branches test.txt shows in finder. But I added text.txt while I was checked out in newBranch, not master.

I feel uneasy about seeing this new text.txt file in the finder directory even when i checkout back to master. Shouldn't it revert to the initial state after cloning. When it only contained file1? And I will see file1 + test.txt when I checkout to the new branch where I actually added it. How can I understand this behavior?

Zoe
  • 27,060
  • 21
  • 118
  • 148

2 Answers2

1

text.txt was never added to git's tracking using git add text.txt or git add .. Therefore, git ignores this file and leaves it as is, changing only files it is tracking.

  • So what you are saying is git is aware of the change. But because I did not add the new file it isn't staged? However, I noticed that when i checked out newBranch and added newFile and than checked out back to master and ran git status newFile was staged. Why is that? This implies to me that staging/adding vs un-staged is a state irrespective to currently checked out branch? – Alexander Bollbach Jan 12 '16 at 00:11
1

Now when I checkout either branches test.txt shows in finder. But I added text.txt while I was checked out in newBranch, not master.

Your working directory and the staging area are "moving" with you to all the branches. They are not related to any branch.

Whenever you change branch only the HEAD is changed. The workdir + index remain unchanged. This is why you see your files.

enter image description here

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • The working directly IS indeed changed. From https://www.atlassian.com/git/tutorials/using-branches/git-branch: "Checking out a branch updates the files in the working directory to match the version stored in that branch...". – Howiecamp Oct 29 '16 at 22:23