4

I have an uploads folder in my project that I need git to track.
Both the folder itself and all subfolders (I mean

uploads,
uploads/users,
uploads/users/profiles,
uploads/users/profiles/pics

and other paths like those) should be tracked.

I have added an empty file named .gitkeep to all folders and my .gitignore is as follows:

uploads/*
!.gitkeep

but git is not tracking the subfolders.

===============================================================

After CodeWizard suggestion for creating .gitkeep files via command and no solution to force git to ignore all files in a directory and childs except the ones having .gitkeep file. i used this command to git-add -f all .gitkeep files created under uploads directory.

find . -name '.gitkeep' | xargs git add -f
Unique
  • 51
  • 1
  • 5
  • did you just create the .gitkeep files or also add them to git? whats' the output of `git status`? – FuzzyAmi Mar 05 '17 at 12:19
  • See http://stackoverflow.com/q/26189082/1256452 and http://stackoverflow.com/q/5533050/1256452 – torek Mar 05 '17 at 13:13

2 Answers2

3

Add .gitkeep to all your required folders.
Otherwize git will not track the folders


Script to create .gitkeep recursively

# find all the directories (-type d)
# filter the empty ones (-empty)
# ignore the .git folder
# add .gitkeep to all the matched folders

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

If you want you can also add it to the most inner folder and git will track all the parent folders as well when add it to the index

The last line (sub1/sub2...) is the output of the above script

enter image description here

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • Your solution doesn't seem to work with the OP's `.gitignore`. It needs to be modified as follows: `uploads/** \n !uploads/users \n !uploads/users/profiles \n !uploads/users/profiles/pics` – Ivan A. Mar 05 '17 at 12:38
  • Im sorry but i don't understand you. What is the problem with the script? i have tested it and its generating the output as seen in the screenshot – CodeWizard Mar 05 '17 at 12:40
  • The problem is, at least on my system, it only tracks the outermost `.gitkeep`, i.e. `uploads/.gitkeep`, _if_ OP's `.gitignore` is used. That's why the `.gitignore` must be modified as well. – Ivan A. Mar 05 '17 at 12:43
  • Yep, this is what it should do. when you check out the branch it will create the full folder structure. It generate `.gitkeep` in the inner most folder. This is enough to keep track of the folders – CodeWizard Mar 05 '17 at 12:45
  • 1
    The problem with this example is `upload` (singular) vs `uploads` (plural). – torek Mar 05 '17 at 12:57
1

While CodeWizard's solution allows you to track the folders, it doesn't help you to ignore their file contents.

To do that, you need to modify your .gitignore to exclude each subfolder from being ignored. For instance:

uploads/**
!uploads/users
!uploads/users/profiles
!uploads/users/profiles/pics

You still need the .gitkeep files inside every subfolder, or at least inside the innermost subfolders. The command suggested by CodeWizard does an excellent job:

find . -type d -empty -not -path "./.git/*" -exec touch {}/.gitkeep \; 
Ivan A.
  • 409
  • 3
  • 9
  • adding all sub folders manually is not my type, CodeWizard command is very useful but it would be great if it add -f the created .gitkeep too. – Unique Mar 18 '17 at 09:17