Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Ignore everything in a directory except subfolders

This is my folder structure:

data/     .gitignore     uploads/         .gitignore 

I would like to commit the folders but not the files inside them.

So I add a .gitignore files in every folder with the following content:

# Ignore everything in this directory * # Except this file !.gitignore 

The problem is that * matches also on directories so git tracks only data/.gitignore

like image 340
nickel715 Avatar asked Sep 21 '13 07:09

nickel715


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 directory in Git?

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.

Can you have more than one Gitignore?

You can have multiple . gitignore , each one of course in its own directory. To check which gitignore rule is responsible for ignoring a file, use git check-ignore : git check-ignore -v -- afile .


2 Answers

Please don't misuse .gitignore files. Better stick to default ways to go on this, so later developers can quickly get into your project.

  1. Add an empty .gitkeep file in the folders that you want to commit without the files
  2. Exclude the folders, but not the .gitkeep from your main .gitignore file.

    folder/* !folder/.gitkeep 

This ignores all files in a folder, but not the .gitkeep file. Now the folder will be commited with only the .gitkeep file as content.

like image 175
kaiser Avatar answered Sep 24 '22 22:09

kaiser


The solution is quite easy, add !*/ to the .gitignore files and only files in the current folder will be ignored

# Ignore everything in this directory * # Except this file !.gitignore # Except folders !*/ 
like image 39
nickel715 Avatar answered Sep 22 '22 22:09

nickel715