1. 程式人生 > >gitignore 例子 只包含特定檔案

gitignore 例子 只包含特定檔案

git工程只上傳必須的檔案,所以需要在一個檔案(跟目錄.gitignore)下面記錄需要忽略的檔案或資料夾。下面舉例.gitignore, 該模版適合去除一些指定目錄,並且僅僅包含其他目錄下的特定結尾的檔案:


*
!/**/
##去除哪些路徑
/dirignore1/
/dirignore2/
##只包含哪些結尾的檔案
!*.c
!*.h
!*.cpp
!*.sh
!*.xml
!*.py
!*.md
!*.jar
!.scala
!.java

1 * 代表忽略全部

2 /** 代表資料夾下的全部內容 ( A trailing "/**" matches everything inside. For example, "abc/**" matches all files inside directory "abc")

為什麼要寫這句,因為不可能包含一個檔案,如果他的資料夾都被忽略了 It is not possible to re-include a file if a parent directory of that file is excluded. (*)

3 包含每個檔案後,/dirignore1/, 在去除不需要的資料夾。(可以注意到.gitignore是按照順序讀入規則的,順序重要)

4 ! 在每行前加感嘆號,表示需要包括後面的匹配檔案或資料夾

5 /**, 連續兩個* 表示包含資料夾下的所有內容 (A trailing "/**" matches everything inside. For example, "abc/**" matches all files inside directory "abc")

6 (只有在沒有 “/” 符號的前提下 , 才會wildcard匹配, 所以模版中不能加"/"這種路徑操作) If the pattern does not contain a slash /, Git treats it as a shell glob pattern and checks for a match against the pathname relative to the location of the .gitignore file


注意: 我查了半天,沒查到如何只包含某個資料夾下的特定檔案的功能

參考:

https://stackoverflow.com/questions/19023550/how-do-i-add-files-without-dots-in-them-all-extension-less-files-to-the-gitign

https://git-scm.com/docs/gitignore