Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write a rule in gitignore to ignore all files except directories in some directory?

Tags:

git

gitignore

So lets say I have a directory structure like so

uploads/
--dir1/
----one.txt
----two.txt
----dir2/
------one.txt
------two.txt

I would like any directory and sub directories of uploads to be tracked but not any files in it with the exception of a dummy file because git doesn't track directories.

I would think something like this would work but it doesn't.

*
!.gitignore
like image 682
David Avatar asked Oct 06 '10 19:10

David


People also ask

Can Gitignore ignore folders?

gitignore is a plain text file in which each line contains a pattern for files or directories to ignore. It uses globbing patterns to match filenames with wildcard characters. If you have files or directories containing a wildcard pattern, you can use a single backslash ( \ ) to escape the character.

Does Gitignore apply to subdirectories?

gitignore file does not exclude the directories unless I fully qualify the ignore pattern as Solution/Project/bin/Debug - which I don't want to do as I will need to include this full pattern for each project in my solution, as well as add it for any new projects added.


1 Answers

When you tell it to ignore *, Git will not recurse into any subdirectories (even though you have a “unignore” pattern that might match something inside one of the directories).

There is a bit of the gitignore pattern language that might help though. A trailing slash forces the pattern to only apply to directories. So, you should be able to use this:

# .gitignore
*
!.keep
!*/

In the demonstration below I use the above .gitignore and have an additional file (named do-not-keep) alongside each .keep file. You can see that it works for multiple levels of subdirectories and does not show the other files. You will have to arrange for each directory to have its own .keep (or whatever) via some independent method.

% git status --short -uall
?? a/.keep
?? a/b/.keep
?? a/b/c/.keep
?? a/b/c/d/.keep
?? e/.keep
?? e/f/.keep
?? e/f/g/.keep
?? h/.keep
?? h/i/.keep
?? j/.keep

This was done with Git 1.7.3.1, but I expect that it will work for other versions as well.

The demonstration setup:

% git init
% mkdir -p a/b/c/d e/f/g h/i j
% zargs -i.. -- **/*(/) -- touch ../.keep ../do-not-keep
% tree -aI .git .                                       
.
|-- .gitignore
|-- a
|   |-- .keep
|   |-- b
|   |   |-- .keep
|   |   |-- c
|   |   |   |-- .keep
|   |   |   |-- d
|   |   |   |   |-- .keep
|   |   |   |   `-- do-not-keep
|   |   |   `-- do-not-keep
|   |   `-- do-not-keep
|   `-- do-not-keep
|-- e
|   |-- .keep
|   |-- do-not-keep
|   `-- f
|       |-- .keep
|       |-- do-not-keep
|       `-- g
|           |-- .keep
|           `-- do-not-keep
|-- h
|   |-- .keep
|   |-- do-not-keep
|   `-- i
|       |-- .keep
|       `-- do-not-keep
`-- j
    |-- .keep
    `-- do-not-keep

10 directories, 21 files
like image 70
Chris Johnsen Avatar answered Sep 29 '22 11:09

Chris Johnsen