How do you get git to ignore all contents of a directory?

Git

Git Problem Overview


I have a git directory which contains the a whole bunch of files and then has a directory called 'sessions'. 'sessions' contains cookie information for my web.py program.

I need the folder 'sessions' to remain in the git repository because without the folder the program does not function correctly. I don't need the actual contents of folder being stored in the git directory.

So the question is:

How can I get git to ignore the contents of a folder but not the folder itself?

Git Solutions


Solution 1 - Git

Add a sessions/.gitignore file with

*
!.gitignore

The second line tells git not to ignore the .gitignore file, so the folder is not empty but everything else is ignored.

Solution 2 - Git

If I'm remembering correctly, you can do this by creating a .gitignore file in the sessions folder with [^.]* as its contents.

Solution 3 - Git

Since July 2007, gitignore does describe the exclusion patterns.

> If the pattern ends with a slash, it is removed for the purpose of the following description, but it would only find a match with a directory. > > In other words, foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how pathspec works in general in git).

As illustrated by this thread, that pattern was not always expressed with a '/' for matching directory.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTabithaView Question on Stackoverflow
Solution 1 - GitMathias VView Answer on Stackoverflow
Solution 2 - GitceejayozView Answer on Stackoverflow
Solution 3 - GitVonCView Answer on Stackoverflow