31

I have one directory tree with many kind of different files. There are 300 directories on the parent directory. Each directory could have other sub directories.

I only want to track *.cocci on all sub directories. Here is my .gitignore:

*
!*.cocci

But it do not work, as the files on sub directories are not tracked. How can I tell git that I only want to track *.cocci on all sub directories?

Peter
  • 1,753
  • 18
  • 19
  • Have you actually `git add`ed the files? Just setting up `.gitignore` does not automatically cause files to be tracked... – twalberg Oct 09 '12 at 14:57
  • I can only add a file if I use git add -f . This do not seem correct... – Peter Oct 09 '12 at 16:08
  • possible duplicate of [Whitelisting and subdirectories in Git](http://stackoverflow.com/questions/9162919/whitelisting-and-subdirectories-in-git) – daviesgeek Oct 28 '13 at 07:23

4 Answers4

49

Read this question.

You want:

# Blacklist everything
*
# Whitelist all directories
!*/
# Whitelist the file you're interested in. 
!*.cocci 

Note, this'll track only *.cocci files. Yours doesn't work because you ignore everything (that's the first line), which ignores all subdirectories.

Community
  • 1
  • 1
simont
  • 68,704
  • 18
  • 117
  • 136
  • 1
    I'm still not happy with git status output as it do not show untracked folder contents but the filter is working now. Thanks – Peter Oct 09 '12 at 16:39
  • 1
    Could you please change the comments in your answer? They must start their own lines. If you paste this into a .gitignore, it will not work (tested on v.1.9.1). Check out http://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository#Ignoring-Files – panepeter Nov 05 '14 at 21:44
  • 3
    You may also want to whitelist .gitignore – Ken A Dec 01 '14 at 13:52
7

To extend @simont answer, if we wanted to whitelist *.cocci files of a given directory and its subdirectories I would wrongly have entered:

directory/*
!directory/*/
!directory/*.cocci 

That seems not to be including *.cocci files under directory/subtree.

In order to find all *.cocci files in all sub-directories of directory entered the following:

directory/**
!directory/*/
!directory/**/*.cocci 
lubumbax
  • 255
  • 2
  • 9
  • 1
    Great! That's what I was finding! To make it clearer, if I want to ignore everything other than `./directory/*`, I should put `*; !directory/; !directory/*`. – Student Mar 28 '20 at 19:17
2

It is impossible to unignore directory by !dir in .gitignore, but then it is still possible to add directory by hand:

git add dir -f

So if the .gitignore file looks like:

*
!*.txt

Then when you do git add . new *.txt files are then added. New directory will not be auto-added, it have to be added by hand by git add dir -f

Milan Kerslager
  • 404
  • 2
  • 6
  • This solved my issue with a directory that was being ignored, even though my `.gitignore` file had a `!dir/.exists` file setup. Note that the `dir` in the answer above should be replaced with the name of the directory that you want to be included in the git checkin. – kenjikato Jul 08 '20 at 08:18
0

Have you tried !*/*.cocci instead of your !*.cocci?

Martin Green
  • 1,056
  • 8
  • 15