I have the folder application/
which I add to the .gitignore
. Inside the application/
folder is the folder application/language/gr
. How can I include this folder?
I've tried this
application/
!application/language/gr/
gitignore file is usually placed in the repository's root directory. However, you can create multiple . gitignore files in different subdirectories in your repository.
A . gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple .
If you exclude application/
, then everything under it will always be excluded (even if some later negative exclusion pattern (“unignore”) might match something under application/
).
To do what you want, you have to “unignore” every parent directory of anything that you want to “unignore”. Usually you end up writing rules for this situation in pairs: ignore everything in a directory, but not some certain subdirectory.
# you can skip this first one if it is not already excluded by prior patterns
!application/
application/*
!application/language/
application/language/*
!application/language/gr/
Note
The trailing /*
is significant:
dir/
excludes a directory named dir
and (implicitly) everything under it.dir/
, Git will never look at anything under dir
, and thus will never apply any of the “un-exclude” patterns to anything under dir
.dir/*
says nothing about dir
itself; it just excludes everything under dir
.
With dir/*
, Git will process the direct contents of dir
, giving other patterns a chance to “un-exclude” some bit of the content (!dir/sub/
). Commit 59856de from Karsten Blees (kblees) for Git 1.9/2.0 (Q1 2014) clarifies that case:
gitignore.txt
: clarify recursive nature of excluded directoriesAn optional prefix "
!
" which negates the pattern; any matching file excluded by a previous pattern will become included again.It is not possible to re-include a file if a parent directory of that file is excluded. (
*
)
(*
: unless certain conditions are met in git 2.8+, see below)
Git doesn't list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.Put a backslash ("
\
") in front of the first "!
" for patterns that begin with a literal "!
", for example, "\!important!.txt
".Example to exclude everything except a specific directory
foo/bar
(note the/*
- without the slash, the wildcard would also exclude everything withinfoo/bar
):
--------------------------------------------------------------
$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
--------------------------------------------------------------
In your case:
application/*
!application/**/
application/language/*
!application/language/**/
!application/language/gr/**
You must white-list folders first, before being able to white-list files within a given folder.
Update Feb/March 2016:
Note that with git 2.9.x/2.10 (mid 2016?), it might be possible to re-include a file if a parent directory of that file is excluded if there is no wildcard in the path re-included.
Nguyễn Thái Ngọc Duy (pclouds
) is trying to add this feature:
So with git 2.9+, this could have actually worked, but was ultimately reverted:
application/
!application/language/gr/
@Chris Johnsen's answer is great, but with a newer versions of Git (1.8.2 or later), there is a double asterisk pattern you can leverage for a bit more shorthand solution:
# assuming the root folder you want to ignore is 'application'
application/**/*
# the subfolder(s) you want to track:
!application/language/gr/
This way you don't have to "unignore" parent directory of the subfolder you want to track.
With Git 2.17.0 (Not sure how early before this version. Possibly back to 1.8.2), using the **
pattern combined with excludes for each subdirectory leading up to your file(s) works. For example:
# assuming the root folder you want to ignore is 'application'
application/**
# Explicitly track certain content nested in the 'application' folder:
!application/language/
!application/language/gr/
!application/language/gr/** # Example adding all files & folder in the 'gr' folder
!application/language/gr/SomeFile.txt # Example adding specific file in the 'gr' folder
I've found only this actually works.
**/node_modules/*
!**/node_modules/keep-dir
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With