0

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?

zhekaus
  • 2,908
  • 3
  • 21
  • 45
  • Check [this](http://stackoverflow.com/questions/115983/how-can-i-add-an-empty-directory-to-a-git-repository) – Deepak Biswal Aug 27 '15 at 09:30
  • @zhekaus This smells a bit like an XY problem. What is it that you're *really* trying to achieve? – Biffen Aug 27 '15 at 10:19
  • @Biffen I have a folder with lots of subfolders that contain lots of different file types. I want particular file types to be placed to git repository. And I don’t want to place to git any other file types. I’ looking gor solutions that will work for every folder structure. For instance I want to place to git *.docx and leave the rest of files. What .gitignore could be in that case? – zhekaus Aug 27 '15 at 10:31

3 Answers3

1

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

Ignore this folder

ProjectNameFolder/bin/*

But not these files

!ProjectNameFolder/bin/project.dll
!*/bin/project.dll
Lali
  • 2,595
  • 4
  • 24
  • 44
  • Folder structure is dynamic. I know nothing about subfolders. – zhekaus Aug 27 '15 at 10:17
  • If you check the default file created by git, you can see all the way to ignore and include files. – Lali Aug 27 '15 at 10:24
  • Pardon? What is a default .gitignore? Where can I get it? – zhekaus Aug 27 '15 at 10:35
  • when you first time pull repository from server, you will receive a default file. – Lali Aug 27 '15 at 10:40
  • No, I'm not. I have no preconfigured default .gitignore in my installation. – zhekaus Aug 27 '15 at 10:41
  • 1
    @Lali That's not true. Git repositories do *not* come with a ‘default’ `.gitignore`. If you get one when you clone/pull then someone must have *manually* added it to the remote repository. – Biffen Aug 27 '15 at 10:43
1

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.

Dmitry VS
  • 475
  • 3
  • 12
  • It demands to remember to put .gitignore to each new folder. It isn’t convenient. The folder structure is changing very often. – zhekaus Aug 27 '15 at 10:21
1

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
Holloway
  • 4,301
  • 1
  • 25
  • 30