2

I have a git repository, and I want git status to ignore all directories (and everything below) if I haven't added files in the directory yet. Adding /* to .gitignore almost does the trick. I can create new directories, put files in them, and git status will be completely oblivious.

So then I create a new directory and put a file there, "directory/file". Then I git add directory, git commit, and create a new file inside this directory, say, "directory/newfile". The problem is that git status won't show me the new file.

I tried adding !/*/* to .gitignore but it didn't do anything as far as I could tell. How can I get git status to show new files in directories with file that are tracked (added), while ignoring directories without tracked files?

Andres Riofrio
  • 9,851
  • 7
  • 40
  • 60

3 Answers3

2

The other answers talk about git not tracking directories, which is correct, but do not answer the question.

First of all, /* not only ignores directories and their content, but also files in the root of your repo. It ignores everything. I am not sure this is what you want. Try changing it /*/ if not.

Second, git add directory would not have worked with the ignore pattern. git add would have failed saying the path is ignored and you have to use -f. And yes, the directory ( and its contents ) can be added with a git add -f directory only.

Now, that you have added and committed the directory, when you put a new file in it, git status will not show you that because, of course, it is being ignored. Git will not step into the directory to look for untracked files. But, of course, modifications to already tracked files will come in git status. Also, git add directory/newfile will fail with the same message about using -f.

So every time you add a directory, you also have to explicitly unignore that particular directory. So you will have to add something like !/directory to your .gitignore.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • Could a commit hook be used to automatically add `!/directory` to `.gitignore`? – Jonas Heidelberg Oct 08 '11 at 21:39
  • @JonasHeidelberg - Yeah, possibly. One can play with `git diff --cached --name-only --diff-filter=A` and add the directory to unignore it from .gitignore I suppose. – manojlds Oct 08 '11 at 21:54
  • Thanks for being so informative and helpful! I'm trying to implement a dotfiles system. This approach might a bit too much trouble, though. :-P I'll probably go with a [RM]akefile-based system. – Andres Riofrio Oct 22 '11 at 23:59
0

Git does not store directories themselves. The file path is an attribute of the node data, if there's no file, there's no node. git add directory should have no effect in any case, if 'directory' is empty. git status would show the newly created file as untracked, except you have it ignored. Try git add directory/newfilename, I'm not sure if .gitignore blocks explicit adds, but I don't think it does. If it does, you'll have to change .gitignore to allow that directory (theres a lot of info on the syntax online).

kylben
  • 349
  • 1
  • 6
0

Git can't track directories. It won't store empty directories, that's why you often find files named ".keep" in projects which want to have "empty"-folders.

Franz Bettag
  • 437
  • 2
  • 8