Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to track directories but not their files with Git?

I've recently started using Git and am having trouble with just one thing. How can I track directories without tracking their contents?

For example the site I'm working on allows uploads. I want to track the uploads directory so that it is created when branching, etc. but obviously not the files within it (test files whilst in develop branch or the real files in master).

In my .gitignore I have the following:

uploads/*.*

Have also tried (which ignores the whole directory):

uploads/

This directory may also contain sub directories (uploads/thumbs/ uploads/videos/) I would like to be able to track these but not their files.

Is this possible with Git? I've searched everywhere without finding an answer.

like image 841
Ben Avatar asked Feb 23 '11 12:02

Ben


People also ask

Can git track a folder?

Git Empty directories in Git Git doesn't track directories Git will only track the file app.

How do I make git not track a file?

Using git update-index to Stop Tracking Files in Git Sometimes, we may wish to keep a file in the repository but no longer want to track its changes. We can use the git update-index command with the --skip-worktree option to achieve this.

Can you git ignore a folder?

. 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.


2 Answers

Git doesn't track directories, it tracks files, so to acheive this you need to track at least one file. So assuming your .gitignore file looks something like this:

upload/* 

You can do this:

$ touch upload/.placeholder $ git add -f upload/.placeholder 

If you forget the -f you'll see:

 $ git add upload/.placeholder The following paths are ignored by one of your .gitignore files: upload Use -f if you really want to add them. fatal: no files added 

Then when you do git status you'll see:

 # On branch master # # Initial commit # # Changes to be committed: #   (use "git rm --cached ..." to unstage) # #   new file:   upload/.placeholder # 

Obviously you can then do:

$ touch upload/images/.placeholder $ git add -f upload/images/.placeholder 
like image 145
Peter Farmer Avatar answered Oct 27 '22 00:10

Peter Farmer


I wrote about this here.

Add a .gitignore within the directory.

like image 30
Abizern Avatar answered Oct 26 '22 22:10

Abizern