I have a very simple git repository that I’ve been using to learn git. I have two branches in addition to master; javafx, netty. I created a new project module within each branch. For the netty branch, the project layout looks like this:
sandbox
netty
For the javafx branch, the project layout looks like this:
sandbox
javafx
Whenever I used to switch between branches, I would still have the directory from the other branch in my working directory. I switched branches a lot and it the extra directory was never deleted. I decided I should learn to clean up my working directory properly and, while on the netty branch, I ran:
git clean -fd
It did exactly what I expected, but now Git’s checkout behavior seems different. When I switch from the netty branch to the javafx branch it’s just like it used to be; the extra netty directory stays in my working directory. However, now when I switch from the javafx branch to the netty branch, the javafx directory gets deleted / cleaned automatically.
However, if I clone the repo on another machine it still works the way it used to; all the untracked files get left in my working directory. Does git clean -fd have some type of permanent effect?
Edit: After some more playing, I have realized this has something to do with my IDE (IntelliJ IDEA). Git can’t seem to delete the directories of active project modules when I check out another branch that doesn’t include those project modules.
For example, if I’m on my netty branch and checkout the javafx branch, Git can’t seem to remove the netty directory. However, if I re-check out the netty branch without regenerating my IDEA project, Git has no problem removing the javafx directory.
After my IDE prevents those directories from being deleted once, they hang around forever as untracked files in my local repo until I run git clean -fd. Even closing my IDE and switching between branches doesn’t change anything.
If I clone the repository and check out branches without ever starting my IDE, everything works like I would expect. The unrelated directories are removed when I check out a new branch.
How does Git react if another process prevents it from removing an unneeded file when a new branch is checked out?
From the man page of git-help
In essence git clean -fd removes all untracked files and directories in your local copy except for the files ignored in .gitignore.
My suspicion is you have started tracking those generated files by mistake in one of those branches.