git and empty folders
Asked Answered
R

9

36

So I see Git doesn't recognize folders, or should I say when the only change between commits is addition of empty folders to the working tree they're not show in git status after git add ..

How would you handle the need to add empty folders to the working tree (for runtime storage) and have them be reflected/created when other repositories pull from the current repository (one in which the folders were added)?

Rebarbative answered 19/11, 2009 at 22:56 Comment(2)
possible duplicate of How do I add an empty directory to a git repositoryCelerity
I've done a little blog post about this here developertipoftheday.com/2011/07/… that might help.Dasteel
H
32

People often put an empty file as a placeholder in the folder to overcome that limitation...

Hypnogenesis answered 19/11, 2009 at 22:58 Comment(3)
personally i'd make it a readme file with some brief explanation of why the folder existsChiquitachirico
@Chiquitachirico that or create the directory when needed. It isn't git's fault your shell scripts or build tool die when a directory doesn't exist.Ubana
@omouse indeed this is usually a workaround for a buggy build or deploy tool/scriptChiquitachirico
U
51

I usually put a .gitignore in those directories as you likely want to ignore any runtime generated data anyway.

Upbraid answered 19/11, 2009 at 23:1 Comment(1)
Another convention is to put a file called ".gitkeep" in the directory. See: https://mcmap.net/q/12621/-what-are-the-differences-between-gitignore-and-gitkeepIntermediacy
H
32

People often put an empty file as a placeholder in the folder to overcome that limitation...

Hypnogenesis answered 19/11, 2009 at 22:58 Comment(3)
personally i'd make it a readme file with some brief explanation of why the folder existsChiquitachirico
@Chiquitachirico that or create the directory when needed. It isn't git's fault your shell scripts or build tool die when a directory doesn't exist.Ubana
@omouse indeed this is usually a workaround for a buggy build or deploy tool/scriptChiquitachirico
R
20

I usually put empty .gitkeep in those folders.

Rooney answered 11/11, 2015 at 15:16 Comment(1)
This is the pattern I was looking forGolliner
O
8

You are absolutely correct. Git, like some other version control systems, does not take cognizance of empty folders or of properties of folders. Folders only exist insofar as there are file that are in them. If you want to simulate this, you need to drop placeholder files into them.

Ocieock answered 19/11, 2009 at 22:59 Comment(0)
H
4

You should realize that you are asking for your source control system to set up some resources that are not source, not part of your build system, but essential to the operation of your application. I would do one of the following:

  1. Make the build create the necessary directory.
  2. Have the application create the required directories as it runs.

Similarly, if the application wanted to append to a log file, it wouldn't make sense to have that logfile start out in the revision control system, would it?

Haney answered 20/11, 2009 at 5:47 Comment(0)
M
3

Either put empty placeholder files into the folders you want git to keep track of or add instructions to create these folders to your build system if possible.

Mariammarian answered 19/11, 2009 at 23:2 Comment(0)
G
0

I am also facing the same issue. I think it has got to do with the way Git tracks changes. It does not track files but rather its contents. When showing the changes, commits or logs it maps the changes to specific files and shows the changes in individual files. See this video for more details Tech Talk: Linus Torvalds on git Linus specifically gives an example about moving a function from one file to another and how git is able to track the change.

Gearalt answered 20/4, 2011 at 18:34 Comment(0)
S
0

For Linux / MAC OSX, use the following one-liner, to create / put empty placeholder files into the folders, recursively, to prevent them from getting ignored, by git:

find . -type d -empty -not -path "./.git/*" -exec touch {}/.gitkeep \;

Hope this helps someone.

Subito answered 28/11, 2023 at 12:24 Comment(0)
A
-2

I faced this problem when trying to use git as a backup tool with support for deduplication and compression.

My solution was to create my own system. It's available on http://github.com/meingbg/store

Again, my purpose was to store files, not work with code.

Amadavat answered 8/12, 2009 at 20:54 Comment(2)
Why don't you just use rsync?Ubana
@omouse: rsync does not track changes. meingbg might be interested in git-annex, a git-based solution that tracks changes in files.Sanitary

© 2022 - 2024 — McMap. All rights reserved.