17

I have a folder in Git with some files in it. Files are a part my Git repository. When running my project some work directories appear in this folder. Directories can have any name and any nesting level with multiple sub-directories. I want to ignore all possible sub-directories appearing in this folder but still keep all files (not directories) sitting in root of my folder.

I've tried patterns:

/
*/
/*
/*/

None of above gives desired result. How it can be accomplished?

Many answers explain how to ignore folders with specific names, for instance Git ignore sub folders. But there seem no explanation on how to ignore all sub-folders with wild cards.

Community
  • 1
  • 1
Boris Zinchenko
  • 2,142
  • 1
  • 24
  • 34
  • Please see [Git ignore sub folders](http://stackoverflow.com/a/40363384/1635042) – Mete Cantimur Dec 21 '16 at 13:44
  • @ Mete Cantimur, pfnuesel, Thank you for the links. I've tried all variants provided there and none worked for me. I do not consider my question as duplicate. People on these links want to ignore sub-folders with specific names, while I want to ignore all possible sub-folders with wild card. – Boris Zinchenko Dec 21 '16 at 13:58

2 Answers2

17

Try this:

!folder/
folder/*

The first line excludes the top level from being ignored, and the second line ignores all subfolders.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 5
    Wouldn't `folder/*/` do the same? – pfnuesel Dec 21 '16 at 13:43
  • @Tim Biegeleisen, Thank you. It works. So I mark it as an answer, although I know this way myself. But in terms of management logic it is not perfect: Suppose I will later move this folder into another place. I will have to adjust .gitignore of parent folder accordingly on every repository restructuring. Is there any way to do it from .gitignore inside the folder in question? – Boris Zinchenko Dec 21 '16 at 13:48
  • 1
    @BorisZinchenko If you start moving files/folders around then `.gitignore` would not be the only problem you would face. You would also have to deal with the history being partially broken as well potential conflicts for anyone who pulls in such changes. – Tim Biegeleisen Dec 21 '16 at 14:14
12

It's quite simple. Create the .gitignore in the folder with:

*/*

I.e. ignore all files which are in any folder of the current folder.

kan
  • 28,279
  • 7
  • 71
  • 101
  • 2
    Thank you for the suggestion. Just tried this and it did not work for me. All sub-folders are still visible. – Boris Zinchenko Dec 21 '16 at 14:25
  • @BorisZinchenko I've tried myself - works fine. I have git v2.10.2. Are you sure you are doing it right? – kan Dec 21 '16 at 16:33
  • I checked our Git version. We run Git-1.8.0. For organizational reasons we cannot update it right now for all our workers. I appreciate your concise answer and will surely use it when we update to newer Git. I cannot mark it as answer because I do not have proper Git at the moment. Sorry. – Boris Zinchenko Dec 21 '16 at 19:44
  • @BorisZinchenko I doubt it's version dependent, most probably you are doing something differently. To be sure try to install a newer version somewhere (your home PC?), create a new repo and check. – kan Dec 21 '16 at 23:01
  • @BorisZinchenko Yes, just checked with 1.7.11.3 - works just fine. One guess - do you use UTF file encoding with BOM for the `.gitignore`? – kan Dec 22 '16 at 11:47
  • I m very grateful for your attention to this issue. Your solution is really concise, elegant, proper and logical in style. I have no idea why it does not work for me but it does not. I've played with file encoding, as you suggested: Unicode, UTF, ANSI - no effect at all. I m testing on Windows 8. For now, we decided for another solution suggested here, which is far less convenient but, luckily, works on our systems. – Boris Zinchenko Dec 22 '16 at 12:09
  • it's amazing how people understand things .. like .. logically the question refers "ignore sub-FOLDERS" (not files) ... facedesk –  Feb 24 '19 at 17:30
  • 1
    @argon git does not track or sees folders. It only tracks full names of files. But now I realize that confusion may be caused that I've said "Create ... _in the folder_ with ..." but he may be was creating it at the working copy root level. – kan Feb 25 '19 at 12:45
  • @kan - this is true only if folders are empty; however, if folders contain anything (-else than empty folders) then git does indeed track them accordingly; so ignoring sub-folders is simple: `/path/to/folder/**/` .. quick note: at the time of this writing Atom Editor does not show this correctly, you'll have to run: `find . -type f | git check-ignore --stdin` from a terminal to see if your ignore pattern works. –  Feb 25 '19 at 12:52
  • 1
    @argon How does `/path/to/folder/**/` differ from `/path/to/folder/*/*`? – kan Feb 26 '19 at 12:48
  • @argon My point is that `**/` works the same as `*/*`. And your amusement about _ignore sub-FOLDERS_ is flimsy. I am repeating myself - git never looks at folders per se, it only could ignore files which may be located in some folders if the file-path matches a pattern. An ignore pattern always defines which *files* should be ignored. – kan Feb 26 '19 at 16:32
  • @kan - if you don't have the common sense to test this yourself, please don't assume everyone else is just as daft .. per se –  Feb 26 '19 at 17:50
  • @argon Works the same for me. If you think I did something daft - demonstrate it. – kan Feb 27 '19 at 07:49