0

I just created a folder like this:

.
├── config
│   ├── subfolder
│   │   └── config.xml
│   └── subfolder2
│       └── config.xml
├── .gitignore
├── log2.txt
└── log.txt

And I've typed git init in this folder. I want to ignore everything except .gitignore, and all config.xml files.

These are the .gitignore I have tried:

1:

*
!.gitignore
!**/config.xml

2:

*
!.gitignore
!config/*/config.xml

3:

*
!.gitignore
!**/config.xml

git status shows .gitignore is the only untracked file with any one of the gitignore settings above.

I want all the config.xml files to be on the untracked files list too.

I've read the tutorial at atlassian. And I think that maybe it's the following rule that makes me not able to ignore everything except certain files under subdirectories.

I'm using git version 2.11.0.windows.1 in windows 10 operating system. Anyone knows why my .gitignore file is not working the way I expect?

enter image description here

Brian
  • 12,145
  • 20
  • 90
  • 153

2 Answers2

0

Cloned your repo.

With this .gitignore:

*
!.gitignore
!*/*/config.xml

It's ignoring everything but .gitignore and config files:

On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   .gitignore
    modified:   config/subfolder/config.xml

no changes added to commit (use "git add" and/or "git commit -a")
.
├── config
│   ├── subfolder
│   │   ├── config.xml
│   │   └── ignoringthis
│   └── subfolder2
│       └── config.xml
├── log3.txt
└── newdir

Is that what you want?

lapinkoira
  • 8,320
  • 9
  • 51
  • 94
0

Thanks to torek's link, I finally come up with the following solution:

/**
!/**/
!.gitignore
!**/config.xml
Community
  • 1
  • 1
Brian
  • 12,145
  • 20
  • 90
  • 153
  • 1
    You don't need all these double asterisks. The following should have the same outcome, at least it works for me with your example repo (although I'm on MacOS and not Windows). Empty spaces stand for newlines since I cannot insert a line break in the comments: `/* !/*/ !.gitignore !config.xml` – gucce Feb 22 '17 at 15:45
  • @gucce you are right. `/* !/*/ !.gitignore !config.xml` works on my computer. – Brian Feb 28 '17 at 13:15