Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unignore specific Files in Subdirectory with .gitignore

Tags:

git

gitignore

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
like image 632
denns Avatar asked Sep 10 '15 14:09

denns


People also ask

Does Gitignore apply to subdirectories?

There are several locations where Git looks for ignore files. Besides looking in the root folder of a Git project, Git also checks if there is a . gitignore in every subdirectory. This way you can ignore files on a finer grained level if different folders need different rules.

How do I ignore a folder in Gitignore?

If you want to maintain a folder and not the files inside it, just put a ". gitignore" file in the folder with "*" as the content. This file will make Git ignore all content from the repository.

How do I unignore a file in Visual Studio?

To remove a file from an Ignore List in Solution Explorer, context-click the file and select Manage Files > Remove from Ignore List.


2 Answers

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.

like image 96
Alexmelyon Avatar answered Sep 21 '22 23:09

Alexmelyon


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).

like image 25
Thomas Stringer Avatar answered Sep 18 '22 23:09

Thomas Stringer