1

Git checkout/pull doesn’t remove directories?

I'm kind of new to git and slowly learning various parts of it.

So forinstance I have a branch

Master with FolderA

I have later from Master created a branch

Dev and added FolderB

So Dev as result have FolderA and FolderB

now when I do git branch master, I'm assuming folder B should be deleted.

But I see FolderB as unstaged folder in master, as I understand it should be removed.

Further more, if I go ahead, when in branch Dev deleting FolderA, when changing branch

to master and back to Dev FolderA appears unstaged in branch Dev.

So why is git not deleting these folders? should I always do some sort of clean command like git clean -f -d?

Or what am I missing?

My real scenario is much more complicated however this simple example should cover the problem I'm experiencing at a larger scale.

Just to be clear: This questions is about content of the branch, it is not concerned with folders only. However the answer is similar to another question pointed out here, but the questions is different.

Zoe
  • 27,060
  • 21
  • 118
  • 148
User123456
  • 671
  • 2
  • 9
  • 19
  • Git doesn't track folders. Are there files in those folders? – mipadi Oct 11 '12 at 20:52
  • No, nothing except files in my ignore. – User123456 Oct 12 '12 at 02:04
  • The answer seems to be the same but the question is arguably not from the same angle. Thanks for the link – User123456 Oct 12 '12 at 02:10
  • What gave me the best answer was from the link @Greg Hewgill posted, Charles Bailey says: As part of most operations that alter the working tree(merge, checkout, etc.) git will remove any directories which are made empty by that operation. In my case as I mentioned there are some ignored files left. Git will not remove any directories that aren't completely empty, if you have hidden or ignored files. So, just to confirm I deleted my ignored files in those directories and now Git removes the directory when switching to another branch not having that directory. This make sense again.. thanks! – User123456 Oct 12 '12 at 02:35

2 Answers2

2

Although Git manages the files within your folders, it doesn't delete the folder itself when switching to a branch where the folder isn't needed. Don't worry about it, you're not missing anything.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • So I should runt git clean -f -d, just to make sure I dont accumulate bunch these folders and confuse my self? But from what you are saying inside of those folder should be empty? where can I find out more about this behavior? – User123456 Oct 12 '12 at 01:52
  • 1
    You can do whatever you need to avoid confusing yourself - if you want to remove those subdirectories, that's fine. You can read more about this here: http://stackoverflow.com/questions/1499157/git-checkout-pull-doesnt-remove-directories – Greg Hewgill Oct 12 '12 at 01:57
0

Note: the ability for Git to remove only empty directories goes back to the very beginning of git:
See commit "Remove empty directories after read-tree -u", from... Oct. 10th, 2005! (seven years ago)

This fixes everybody's favorite gripe that switching branch with 'git checkout' leaves empty directories.

Signed-off-by: Junio C Hamano <junkio@cox.net>

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250