I have one branch (let\'s call it B) that ignores a certain file, which isn\'t ignored in some other branches (eg branch A). When i switch from branch B to branch
If you ignore the file only in branch B but not in branch A, then you can't prevent it from becoming deleted when switching from A to B.
However, from you comments I see that you try to ignore the file in both branches, but that it still gets deleted. Its probably still tracked in A. Go to branch A and
removed the ignored file from the index
git rm --cached
Careful if you push branch A into your repository. This causes the file to get deleted on repository and other developers machines on their next git pull but not on your local. You may add the file after the pull on these machines.
I had the exact same issue. Here is how it happened to me:
test.json was once tracked in B. A from B (so the file test.json also tracked in A). test.json in branch B. However git will continue to track any files that are already being tracked. To stop tracking the file, I removed it from the index git rm --cached foo/test.json.After these 3 steps the following happes: If I am in branch B the file test.json exists. If I switch to A it still exists. If I checkout back to B its gone. So I noticed the same as you did:
I can sort of see how it would happen, in the sense that branch B thinks it's not there, and branch A thinks that it is, so when i go back to B it 'tidies it away'. But it's kind of annoying.
I added test.json in the .gitignore file from branch A but it still got deleted when switching back to B. And this is, because it was tracked in A before I ignored it.
Thus you need to remove it from the index in branch A. Then its ignored and untracked by A and B and it does not get deleted when you switch back from A to B.