Git Ignore everything in a directory except subfolders

巧了我就是萌 提交于 2019-11-29 04:45:01

问题


This is my folder structure:

data/
    .gitignore
    uploads/
        .gitignore

I would like to commit the folders but not the files inside them.

So I add a .gitignore files in every folder with the following content:

# Ignore everything in this directory
*
# Except this file
!.gitignore

The problem is that * matches also on directories so git tracks only data/.gitignore


回答1:


Please don't misuse .gitignore files. Better stick to default ways to go on this, so later developers can quickly get into your project.

  1. Add an empty .gitkeep file in the folders that you want to commit without the files
  2. Exclude the folders, but not the .gitkeep from your main .gitignore file.

    folder/*
    !folder/.gitkeep
    

This ignores all files in a folder, but not the .gitkeep file. Now the folder will be commited with only the .gitkeep file as content.




回答2:


The solution is quite easy, add !*/ to the .gitignore files and only files in the current folder will be ignored

# Ignore everything in this directory
*
# Except this file
!.gitignore
# Except folders
!*/



回答3:


Try this:

*.*
!.gitignore
!*/*


来源:https://stackoverflow.com/questions/18930103/git-ignore-everything-in-a-directory-except-subfolders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!