zhekaus
zhekaus

Reputation: 3194

Is there a way to ignore files only but not folders?

I have a folder with lots of subfolders that contain lots of different file types. I want particular file types to be added to git repository. And I don’t want to add any other file types. I’ looking for solutions that will work for every folder structure.

Thus I’m trying to exclude all the files containing in the folder structure except of particular patterns. The problem is, as I understand, git doesn’t look into ignored folders.

For instance, let's say I want to place to git *.docx and ignore the rest of files. Could anybody say what .gitignore should be in this case?

Upvotes: 0

Views: 131

Answers (3)

Lali
Lali

Reputation: 2866

Use '!' before the path of file in .gitignore

Ignore this folder

ProjectNameFolder/bin/*

But not these files

!ProjectNameFolder/bin/project.dll
!*/bin/project.dll

Upvotes: 1

Holloway
Holloway

Reputation: 7377

Exclude everything, then unexclude the files you do want.

*.*
!*.docx

eg with the dir structure:

|-- a
|   |-- aa
|   |   |-- asd.docx
|   |   `-- sef.fvd
|   |-- ab
|   |-- ferfe.docx
|   `-- refdv.dsfv
|-- b
|   |-- a
|   |   |-- fsef.egrd
|   |   `-- sree.docx
|   |-- eser.docx
|   `-- fefsd.dfv
|-- bhy.vc
|-- rgt.d
|-- rsh.vdf
|-- sdf.nh
`-- sfsfd.docx

Adding everything gives:

$/tmp/exc: git add .
$/tmp/exc: git status
On branch master

Initial commit


Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

new file:   a/aa/asd.docx
new file:   a/ferfe.docx
new file:   b/a/sree.docx
new file:   b/eser.docx
new file:   sfsfd.docx

Upvotes: 1

Dmitry VS
Dmitry VS

Reputation: 615

You can put .gitignore file into each folder you want to track but do not want to track files inside it. Contents of .gitignore should be:

*
!.gitignore

In this case all the files inside those folder will be ignored but folders themselves will be tracked by git. The only drawback is that .gitignore files will also be tracked.

Upvotes: 1

Related Questions