Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I git add a folder but none of its contents?

Tags:

git

github

is there an easy way to git add a folder but none of its contents?

For example, if I'm developing a website with an images/ folder, I don't want the heavy images to be added to github, but I would like the folder to be added, because it makes copying the repo easier for other people / me on other computers.

One way to almost do this is to just modify my git-ignore file so that certain types of files (like jpegs) are ignored, but if all the files in my folder are jpegs, and all of those are ignored, then github will ignore the folder as well - which is not what I want.

Thus, I'm wondering if instead there is a one-line command that will add a folder but not its contents, ideally without having to add an empty placeholder file in every folder I want to add to github.

Thanks!

EDIT: From further reading, it looks like I do have to add a placeholder file like '.placeholder' or '.empty' to each folder that I want git to notice.

like image 696
Hillary Sanders Avatar asked Oct 21 '14 05:10

Hillary Sanders


Video Answer


2 Answers

You should be able to drop a .gitignore file in each dir you want to keep (and want to keep empty, if I'm understanding the intent). You could opt to have the file itself contain !.gitignore, per https://stackoverflow.com/a/11579945/1795230. Then add/commit that .gitignore.

The presence of that file should keep your folder saved in git, and the entry to ignore everything that is not itself should keep the heavy images you mention our of the folder in your remote repo as well. So, a twofer, which is nice.

like image 192
Brian Henry Avatar answered Sep 21 '22 22:09

Brian Henry


I'd say create something like a .keep file and then do "git add my/dir/.keep".

Git ignores empty folders, so there must be at least one file in your folder so git doesn't ignore it.

like image 21
Sebastian Osuna Avatar answered Sep 20 '22 22:09

Sebastian Osuna