Gitignore all folders beginning with a period
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.
.**/
Related videos on Youtube
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, 2022Comments
-
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 about 8 yearsIt is not a duplicate of that question as here only dot-folders are wanted, not also dot-files.
-
yu yang Jian over 3 yearsI need to commit new gitignore file first to make it take effect
-
-
Vampire about 8 yearsIncluding
.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 about 8 yearsShouldn't
.*/
be enough? Should work afair. Update: yes, it is -
CodeWizard about 8 yearsYou are right, i missed the folders. it should be ** for folders :-)
-
Vampire about 8 yearsNo. 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 about 8 yearsI know, i showed him the patterns, he can grab what he need. i agree
-
Vampire about 8 yearsWell, 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 about 8 yearsOk. feel free to update the answer if in wrong. Thank you
-
eddiemoya about 8 yearsIt 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 about 8 yearsTry 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 almost 6 yearsCould 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?