3

I have two branches: master and web and I have my working copy in one directory. In web branch I've the folder foo. I worked on foo's files and I committed the job. Then I switched to master with:

git checkout master

But foo folder is still there in my working copy. It looks strange! Git status on master branch tells me the all is fine. But the folder foo does not belong to master branch. Even If I do

git checkout .

web folder stays in my working copy. To fix I rm web folder. Now, git status keeps telling me the everything is fine and so it is.

But it's very strange, what could have happened and how can I avoid that?

floatingpurr
  • 7,749
  • 9
  • 46
  • 106
  • 1
    Have a look at the [git clean](https://git-scm.com/docs/git-clean) command . Git is a file tracker and so a folder will still be sitting there when you switch branches. If you do the git clean with the -d option it will remove directories, but I strongly recommend git clean -n to see what will happen before you do other variants! – rasmeister Jan 29 '17 at 16:02
  • 2
    What it looks strange to me is that usually my working copy is modified every time I switch to a branch. All folders of the old branch vanish and new folder appears. That's what I want to do but that's not the case of the problem I've described. – floatingpurr Jan 29 '17 at 16:06
  • I found a potential explanation here: https://stackoverflow.com/questions/3885850/git-not-removing-files-when-switching-branch/22169659 – floatingpurr May 14 '19 at 23:23

1 Answers1

1

I think you have a file in foo that is commited to master as well. Compare the output of

git checkout web
git ls-files foo

with

git checkout master
git ls-files foo

The ls-files command should return empty if the folder is not versioned. But I think you will get output anyway.

Git does keep track of which folders are exclusive to a branch and will remove them when switching branches. git status also reports on all untracked files that are not in the .gitignore.

Harald Nordgren
  • 11,693
  • 6
  • 41
  • 65
  • `git ls-files foo` is empty in `master`. 'foo' has always been exclusive to `web`. I'm using git carefully, then I do not understand how this happened. Could be atom fault? I'm switching through branch via bash but I'm visualizing the project in atom... – floatingpurr Jan 29 '17 at 16:19
  • What is the output of `ls -al foo` from the master branch? – Harald Nordgren Jan 29 '17 at 16:20
  • `No such file or directory` but, as I described, I `rm` the folder manually to keep my working dir aligned to the last `master` commit. – floatingpurr Jan 29 '17 at 16:25
  • Could you checkout web and then checkout master again, and try the `ls-files` and `ls -al` commands before the `rm`? – Harald Nordgren Jan 29 '17 at 17:14
  • I've already removed files. I do not understand how it was possible. Maybe a problem with git management of Atom? – floatingpurr Jan 29 '17 at 17:21
  • Potential explanation here: https://stackoverflow.com/questions/3885850/git-not-removing-files-when-switching-branch/22169659 – floatingpurr May 14 '19 at 23:23