62

I want to use a .gitignore file to ignore all folders beginning with a period (hidden folders of linux).

I can't figure out the syntax, though I'm sure it's simple.

How's it done?

dreftymac
  • 31,404
  • 26
  • 119
  • 182
cjm2671
  • 18,348
  • 31
  • 102
  • 161

3 Answers3

89

Use one of these patterns:

# ignore all . files but include . folders
.*
!.*/

# ignore all . files and . folders
.*

# Dont ignore .gitignore (this file)
# This is just for verbosity, you can leave it out if
# .gitignore is already tracked or if you use -f to
# force-add it if you just created it
!/.gitignore

# ignore all . folders but include . files
.*/

What is this pattern?

.* - This patter tells git to ignore all the files which starts with .

! - This tells git not to ignore the pattern. In your case /.gitignore

A demo can be found in this answer:
Git: how to ignore hidden files / dot files / files with empty file names via .gitignore?

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • Including `.gitignore` explicitly is probably descriptive, but unnecessary if .gitignore is already tracked, or added with `-f`. :-) Besides that, it does not answer the question as OP just asked for folders, not files – Vampire Apr 28 '16 at 15:39
  • You are right, i missed the folders. it should be ** for folders :-) – CodeWizard Apr 28 '16 at 15:45
  • No. he wanted **only** folders and your second thing does the same as the first. `.*` will match anything starting with a dot (files *and* folders), `.*/` will match anything starting with a dot that is a folder – Vampire Apr 28 '16 at 15:47
  • I know, i showed him the patterns, he can grab what he need. i agree – CodeWizard Apr 28 '16 at 16:03
  • Well, he can not. As he accepted I guess he didn't mean what he said. But your answer is plainly wrong. `.*` does not ignore all dot-files, but dot-files and dot-folders and `.**` should be the same. Ignoring only the dot-folders would still be `.*/`. :-) – Vampire Apr 28 '16 at 21:20
  • Ok. feel free to update the answer if in wrong. Thank you – CodeWizard Apr 28 '16 at 21:24
9

.*/ will match everything that starts with a dot and is a folder

With the following command you can test it: mkdir test && cd test && git init && mkdir -p .foo .foo/.bar foo/.bar && touch .foo/dummy .foo/.bar/dummy foo/.bar/dummy && git add . && git status && echo '.*/'>.gitignore && git reset && git add . && git status

Vampire
  • 35,631
  • 4
  • 76
  • 102
3

You should be able to use the double asterisk wildcard, which represents directories at any depth.

.**/
eddiemoya
  • 6,713
  • 1
  • 24
  • 34
  • 3
    Shouldn't `.*/` be enough? Should work afair. Update: yes, it is – Vampire Apr 28 '16 at 15:40
  • It would be if he only wants the first level of directories to be ignored. He said "any" though. This would include directories at directory depth. – eddiemoya Apr 29 '16 at 15:10
  • 2
    Try the following command and you will see it works fine: `mkdir test && cd test && git init && mkdir -p .foo .foo/.bar foo/.bar && touch .foo/dummy .foo/.bar/dummy foo/.bar/dummy && git add . && git status && echo '.*/'>.gitignore && git reset && git add . && git status` – Vampire Apr 29 '16 at 15:20
  • Could you give an example? Let's say I have folder named `IgnoreMe` and it appears with the same name in various places and depths in the repository. How could I ignore its content? – Royi May 26 '18 at 12:42