1. 程式人生 > >Git 忽略資料夾下的檔案,但是保留空資料夾

Git 忽略資料夾下的檔案,但是保留空資料夾

最近在做專案發現,想用 git 忽略掉所有日誌檔案,但是發現那樣也會忽略掉日誌資料夾,這樣顯得專案的結構不夠完整,於是找到如下解決方法.

假設專案資料夾結構如下:

smartlock
|-- conf
    `-- nginx.conf
    `-- mime.types
    `-- fastcgi.conf
|-- logs
|-- html
    `-- css
    `-- js
    `-- images

想要忽略掉 logs 資料夾下所有的日誌檔案,但是想保留 logs 資料夾以保持專案資料夾結構的完整性。可以在 logs 資料夾裡新建 .gitignore 檔案,檔案內容如下:

# ignore all except .gitignore file
*
!.gitignore

然後 git add . ,就可以將 logs 資料夾新增進 git 快取了。