Mike Christensen
Mike Christensen

Reputation: 91666

Switching branches in Git doesn't remove directories

I've created two branches in Git:

git branch F1
git branch F2

Next, I switch over to F1:

git checkout F1

I rename my UnitTests directory to Tests:

git mv .\UnitTests .\Tests
git commit . -m "Moving unit tests to Tests directory"

At this point, I have a master branch with a UnitTests directory, an F2 branch with a UnitTests directory and a F1 branch with a Tests directory. So, I switch over to F2:

git checkout F2

When I do this, I now see the UnitTests directory added back, but I also see the Tests directory (From the F1 branch) still there. It even has all the same files.

I can force it to be removed by running:

git clean -fd

However, I'm wondering why remnants from the old branch are lingering around. I've looked at a few other potential duplicates, but all of them note cases where my move was not yet committed. I clearly committed the move to F1 before switching branches.

Any ideas? I'm running git version 1.9.2.msysgit.0 on Windows.

Upvotes: 10

Views: 2915

Answers (3)

Mike Christensen
Mike Christensen

Reputation: 91666

I believe the other answers are mostly correct, but not quite. Figured I would add my own answer in case it helps any other Git newbies.

First off, I checked for ignore rules and for hidden files, but none of either turned up.

What is getting left behind in Tests when I switch over to F2 is empty directories. They're not ignored or hidden or anything, they were just never checked in due to being empty. It seems the checkout command will remove all the versioned files, but leave the empty directories alone. The clean command will clean these up.

Looks like Git is working as expected. Thanks!

Upvotes: 4

Cezary Baginski
Cezary Baginski

Reputation: 2115

Try with a new directory with one file inside.

I'm guessing you had maybe some hidden or ignored files (or subdirectories) in the test directory.

Git doesn't care about directories - only about content inside them, so renaming a directory is pretty much the same as "renaming" the files inside.

This is why you can't git add on an empty directory - and it doesn't show up in git status.

A good way to check is: git clean -ndx (shows both ignored files and empty directories).

Upvotes: 1

Udy
Udy

Reputation: 2542

It is because you have files or directories (could be empty) that git ignore (hidden or explicit ignored) in the UnitTests directory

Therefore, git wouldn't delete these files so he keeps them in the directory.

Upvotes: 3

Related Questions