开发者

How to .gitignore all files/folder in a folder, but not the folder itself? [duplicate]

开发者 https://www.devze.com 2023-01-26 04:15 出处:网络
This question already has answers here: 开发者_JAVA技巧 How do I add an empty directory to a Git repository?
This question already has answers here: 开发者_JAVA技巧 How do I add an empty directory to a Git repository? (37 answers) Closed 1 year ago.

I want to check in a blank folder to my Git repository. Effectively, I need to ignore all of the files and folders within the folder, but not the folder itself. How can I do this? What should I put in my .gitignore file?

For those wondering why I would want to do this, I have an "upload" directory in my repository. I want to commit the blank directory, but without all the contents.


Put this .gitignore into the folder, then git add .gitignore.

*
!.gitignore

The * line tells git to ignore all files in the folder, but !.gitignore tells git to still include the .gitignore file. This way, your local repository and any other clones of the repository all get both the empty folder and the .gitignore it needs.


You can't commit empty folders in git. If you want it to show up, you need to put something in it, even just an empty file.

For example, add an empty file called .gitkeep to the folder you want to keep, then in your .gitignore file write:

# exclude everything
somefolder/*

# exception to the rule
!somefolder/.gitkeep 

Commit your .gitignore and .gitkeep files and this should resolve your issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消