Reputation: 1131
I have a problem to get .gitignore to do what I want. My folder structure looks like so:
assets
├── img
| ├── thousands
| ├── of
| ├── folders
| ├── KEEP_SOMETHING_IN_THIS_FOLDER
| | ├── another
| | ├── thousands
| | ├── of
| | ├── folders
| | ├── KEEP_THIS_FILE_1.jpg
| | ├── KEEP_THIS_FILE_2.jpg
| | ├── KEEP_THIS_FILE_3.jpg
I try to keep the three jpgs. I tried
/assets/img/*
!/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/
/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/*/
!/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/KEEP_THIS_FILE_*.jpg
Upvotes: 22
Views: 14748
Reputation: 1239
You need to create .gitignore file in specified folder. In "KEEP_SOMETHING_IN_THIS_FOLDER" in your case. And write this lines:
/**
/*.jpg
!not_ignore.jpg
At first commit this .gitignore
file and then try commit your files.
But if your files already has been staged (tracked), try git rm --cached <filePath>
these files.
Upvotes: 20
Reputation: 339
you can do like this:
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
and like:
# exclude /filepath/includes/a.java
/*
!/filepath
/filepath/*
!/filepath/includes
/filepath/includes/*
!/filepath/includes/a.java
by the way If you wanna ignore all jar (*.jar), but wanna exclude
/src/main/webapp/WEB-INF/lib/*.jar
Do this in your .gitignore
# Package Files #
*.jar
# exclude /src/main/webapp/WEB-INF/lib/*.jar
!/src/main/webapp/WEB-INF/lib/*.jar
Upvotes: 7
Reputation: 765
When running man gitignore
from a *nix workstation, and looking at the PATTERN FORMAT
section, I found the following statement:
- An 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. 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".
Notice the bolded section. In the case you presented, where you want to unignore files that fall under a sub-directory of the directory you are ignoring, the "un-ignore" !
operator will not function. Instead, you must be more specific with the ignore pattern.
Try this on for size:
/assets/img/*/*
!/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/KEEP_THIS_FILE*.jpg
Upvotes: 15
Reputation: 5862
You were close:
/assets/img/*
!/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/
# changed this:
# /assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/*/
# to:
# /assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/*
/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/*
!/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/KEEP_THIS_FILE_*.jpg
You don't need the trailing slash on the ignore for the child folder (the 3rd line).
Upvotes: 11