Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to .gitignore all files/folder in a folder, but not the folder itself? [duplicate]

Tags:

git

gitignore

I want to check in a blank folder to my Git repository. Effectively, I need to ignore all of the files and folders within the folder, but not the folder itself. How can I do this? What should I put in my .gitignore file?

For those wondering why I would want to do this, I have an "upload" directory in my repository. I want to commit the blank directory, but without all the contents.

like image 233
Aron Woost Avatar asked Nov 22 '10 20:11

Aron Woost


People also ask

Does Gitignore work in subfolders?

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

Does Gitignore need to be in every folder?

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 .


2 Answers

Put this .gitignore into the folder, then git add .gitignore.

* */ !.gitignore 

The * line tells git to ignore all files in the folder, but !.gitignore tells git to still include the .gitignore file. This way, your local repository and any other clones of the repository all get both the empty folder and the .gitignore it needs.

Edit: May be obvious but also add */ to the .gitignore to also ignore subfolders.

like image 121
Trianam Avatar answered Sep 30 '22 18:09

Trianam


You can't commit empty folders in git. If you want it to show up, you need to put something in it, even just an empty file.

For example, add an empty file called .gitkeep to the folder you want to keep, then in your .gitignore file write:

# exclude everything somefolder/*  # exception to the rule !somefolder/.gitkeep  

Commit your .gitignore and .gitkeep files and this should resolve your issue.

like image 33
kubi Avatar answered Sep 30 '22 17:09

kubi