Gitignore all folders beginning with a period

37,978

Solution 1

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?

Solution 2

.*/ 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

Solution 3

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

.**/
Share:
37,978

Related videos on Youtube

cjm2671
Author by

cjm2671

Founder of WikiJob, UK's largest graduate careers website. https://www.wikijob.co.uk. Founder of Linkly, https://linklyhq.com - click tracking software for marketers.

Updated on July 09, 2022

Comments

  • cjm2671
    cjm2671 almost 2 years

    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?

    • Vampire
      Vampire about 8 years
      It is not a duplicate of that question as here only dot-folders are wanted, not also dot-files.
    • yu yang Jian
      yu yang Jian over 3 years
      I need to commit new gitignore file first to make it take effect
  • Vampire
    Vampire about 8 years
    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
    Vampire about 8 years
    Shouldn't .*/ be enough? Should work afair. Update: yes, it is
  • CodeWizard
    CodeWizard about 8 years
    You are right, i missed the folders. it should be ** for folders :-)
  • Vampire
    Vampire about 8 years
    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
  • CodeWizard
    CodeWizard about 8 years
    I know, i showed him the patterns, he can grab what he need. i agree
  • Vampire
    Vampire about 8 years
    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 .*/. :-)
  • CodeWizard
    CodeWizard about 8 years
    Ok. feel free to update the answer if in wrong. Thank you
  • eddiemoya
    eddiemoya about 8 years
    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.
  • Vampire
    Vampire about 8 years
    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
  • Royi
    Royi almost 6 years
    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?