How to track directories but not their files with Git?

前端 未结 5 2073
礼貌的吻别
礼貌的吻别 2020-12-02 10:22

I\'ve recently started using Git and am having trouble with just one thing. How can I track directories without tracking their contents?

For example the site I\'m wo

5条回答
  •  爱一瞬间的悲伤
    2020-12-02 10:58

    In order to track only directories but not the files, I did the following. Thanks to the @PeterFarmer's comment on git tracking only files, I've been able to keep all directories excluding the files as described in below.

    # exclude everything in every folder
    /data/**/*.*
    
    # include only .gitkeep files
    !/data/**/*.gitkeep
    

    Adding this to the .gitignore file will do the work. The following is my folder structure.

    data/
    ├── processed
    │   ├── dataset1.csv
    │   └── dataset2.csv
    ├── raw
    │   ├── raw_dataset1.json
    └── test
        ├── subfolder
        │   └── dataset2.csv
        └── reviews.csv
    

    When I do git add . && git status, git only recognizes folders, but not files.

    Changes to be committed:
      (use "git reset HEAD ..." to unstage)
    
            modified:   .gitignore
            new file:   data/processed/.gitkeep
            new file:   data/raw/.gitkeep
            new file:   data/test/.gitkeep
            new file:   data/test/subfolder/.gitkeep
    

    Keep in mind that the following for your .gitignore files:

    Prepending slash looks only for the root directory.

    /dir

    Double asterisk looks for zero or more directories.

    /**/

提交回复
热议问题