2

I have a directory structure similar to this:

root/
  .gitignore
  subdir/
    .gitignore
    subsubdir1/
      file.xml
      image.png
    subsubdir2
       file.xml
       image.jpg

I want to ignore all files from subdir except XML files. the second .gitignore looks like this:

*                   # ignore everything in this directory and its subdirectories
!*/                 # do not ignore subdirectories
!*.xml              # do not ignore xml files
!.gitignore         # and of course have this .gitignore file included too

according to this and various other posts here on SO and other sites, this should work just fine, but for some reason git status shows me that all the files in subdir folder are untracked.

I have used git rm --cached to remove all files that have been tracked before .gitignore was added, but that still did not do the trick.

Do you have any clue, what would fix this ?

P.S.: In case it matters I am using git on Windows.

Community
  • 1
  • 1
jcxz
  • 1,226
  • 2
  • 12
  • 25

1 Answers1

3

.gitignore does not support inline comments. Comments need to start on their own line.


You need to remove the !*/ this will end up undoing the line before it.

If you want to ignore everything in subdir but not in subsubdirs then that line should be:

!/*

If you want to ignore files like subdir/subsubdir2/image.jpg then you can just leave that line out.

Empty directories (or where all files inside are ignored) won't be added to git.

I think (and confirmed on my linux system) that .gitignore is automatically excluded so you can omit the last line.

This leaves in subdir/.gitignore:

*
!*.xml
Shawn Balestracci
  • 7,380
  • 1
  • 34
  • 52
  • I don't know there is something fundamentally broken with my repository, because now I tried to leave only the first rule in my gitignore (i.e. only the star) and it just completely disregards me (I am doing `git status` and all the changes from the root directory if that matters somehow ...) – jcxz Apr 19 '13 at 21:54
  • If you changed your root/.gitignore the !*/ in subdir/.gitignore would still override it. – Shawn Balestracci Apr 19 '13 at 22:05
  • no I have changed the `.gitignore` from `subdir` folder, but while fixing this problem I have done several times `git reset HEAD` (yet before posting here). I don't know if that changes anything, I am already trying anything possible as I have no idea, why this is happening – jcxz Apr 19 '13 at 22:15
  • 1
    OK, so the directories in my example had just stub names to demonstrate my problems. Here is the actual structure of `subdir` folder: [link](http://pastebin.com/DwE6qfuv) Here is the `git status` output: [link](http://pastebin.com/fYwaXP0C) Here is the first `.gitignore`: [link](http://pastebin.com/21fwr7hA) And here is the second one: [link](http://pastebin.com/VuGLK9mC) – jcxz Apr 19 '13 at 22:43
  • Oh, I see now. .gitignore does not support inline comments. Comments have to start on their own line. – Shawn Balestracci Apr 19 '13 at 22:50
  • Yes, Thanks! I have just realised that too, after posting all those files. Please put that in your answer and I will mark it as accepted. – jcxz Apr 19 '13 at 22:52