17

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.

1

2 Answers 2

19

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.

Sign up to request clarification or add additional context in comments.

1 Comment

usually this would be called ".gitkeep"
11

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.

1 Comment

Much better way, if You do not want any content in that folder to be commited by mistake. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.