Unignore specific files in a subdirectory with .gitignore

I have a problem to get .gitignore to do what I want. My folder structure looks like this:

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 am trying to save three jpg. 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
+4
source share
3 answers

You need to create a .gitignore file in the specified folder. In "KEEP_SOMETHING_IN_THIS_FOLDER" in your case. And write the following lines:

/**
/*.jpg
!not_ignore.jpg
+3
source

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 to ignore the child folder (3rd line).

+3
source

man gitignore * nix PATTERN FORMAT, :

  • "!" ; , . , . Git , , , . ( "\" ) "!" , "!" , , "! important!.txt".

Pay attention to the selected area. In the case you have presented, where you want to unlock files that fall under a subdirectory of a directory that you ignore, the un-ignore operator !will not function. Instead, you should be more specific with the ignore pattern.

Try this for size:

/assets/img/*/*
!/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/KEEP_THIS_FILE*.jpg
+1
source

Source: https://habr.com/ru/post/1606801/


All Articles