Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitignore exclude certain files in all subdirectories

Tags:

git

gitignore

I have a directory called /static. There is a lot of subdirectories in it. I need to ignore all files in all subdirectories of the /static/ directory except of .htaccess, null.jpg and index.php files. Tell me please what is the right syntax for this operation?

/static/**
!/static/**/.htaccess

and

/static/*
!/static/*/.htaccess

don't work.

like image 742
Vladimir Mikhaylovskiy Avatar asked May 27 '15 03:05

Vladimir Mikhaylovskiy


People also ask

Does Gitignore apply to subdirectories?

gitignore file is usually placed in the repository's root directory. However, you can create multiple . gitignore files in different subdirectories in your repository.

How do I ignore a specific file in git?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a .gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

How do I ignore a specific folder in git?

So putting a . gitignore with contents /bin will make it ignore files or folders named bin in th esame folder as the . gitignore . If you want to specify that bin should be a folder, then put a trailing slash.

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.


1 Answers

As I mentioned in "Including specific file extension in gitignore", the main rule to remember is:

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.?+, see below)

That is why any rule which ignores folders (like * or */) would make excluding any sub-files impossible.

That is why the right approach is to exclude everything except:

  • folders,
  • (then) the files you want to exclude.

If you don't exclude folders first, your files would still be ignored (because of the rule I mention above)

So add in your .gitignore:

/static/**/**
!/static/**/
!.gitignore
!.htaccess

This is tested with Git 2.4.1 and works even on Windows.


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:

  • commit 506d8f1 for git v2.7.0, reverted in commit 76b620d git v2.8.0-rc0
  • commit 5e57f9c git v2.8.0-rc0,... reverted(!) in commit 5cee3493 git 2.8.0-rc4.

However, since one of the conditions was "The directory part in the re-include rules must be literal (i.e. no wildcards)", you cannot use that feature here anyway.

like image 134
VonC Avatar answered Oct 13 '22 05:10

VonC