有些時候,你必須把某些文件放到Git工作目錄中,但又不能提交它們,比如保存了數據庫密碼的配置文件啦,等等,每次git status都會顯示Untracked files ...。
解決的方法就是在git的根目錄下編寫一個.gitinnore文件。
原則
GitHub已經為我們准備了各種配置的.gitignore文件,只需要組合一下就可以使用了。所有配置文件可以直接在線瀏覽:https://github.com/github/gitignore
忽略文件的原則是:
- 忽略操作系統自動生成的文件,比如縮略圖等;
- 忽略編譯生成的中間文件、可執行文件等,也就是如果一個文件是通過另一個文件自動生成的,那自動生成的文件就沒必要放進版本庫,比如Java編譯產生的
.class文件; - 忽略你自己的帶有敏感信息的配置文件,比如存放口令的配置文件。
文件語句的語法規則
空行, 不匹配任何文件,所以可以添加空行,用來提高文件的可讀性。
#, 開頭的行是注釋。Put a backslash ("\") in front of the first hash for patterns that begin with a hash.
一行語句后面的空格,將被忽略,除非它們用反斜杠(“\”)括起來。
An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined. Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example, "\!important!.txt"
If the pattern ends with a slash, it is removed for the purpose of the following description, but it would only find a match with a directory. In other words, foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how pathspec works in general in Git).
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 (relative to the toplevel of the work tree if not from a .gitignore file).
Otherwise, Git treats the pattern as a shell glob: "*" matches anything except "/", "?" matches any one character except "/" and "[]" matches one character in a selected range. See fnmatch(3) and the FNM_PATHNAME flag for a more detailed description.
A leading slash matches the beginning of the pathname. For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".
Two consecutive asterisks ("**") in patterns matched against full pathname may have special meaning:
A leading "**" followed by a slash means match in all directories. For example, "**/foo" matches file or directory "foo" anywhere, the same as pattern "foo". "**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo".
A trailing "/**" matches everything inside. For example, "abc/**" matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth.
A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.
Other consecutive asterisks are considered regular asterisks and will match according to the previous rules.
示例
忽略Windows自動生成的垃圾文件:
# Windows: Thumbs.db ehthumbs.db Desktop.ini
然后,繼續忽略Python編譯產生的.pyc、.pyo、dist等文件或目錄:
# Python: *.py[cod] *.so *.egg *.egg-info dist build
加上你自己定義的文件:
# My configurations: db.ini deploy_key_rsa
最后一步就是把.gitignore也提交到Git,就完成了!當然檢驗.gitignore的標准是git status命令是不是說working directory clean。、
如果你確實想添加該文件,可以用-f強制添加到Git:
$ git add -f App.class
或者你發現,可能是.gitignore寫得有問題,需要找出來到底哪個規則寫錯了,可以用git check-ignore命令檢查:
$ git check-ignore -v App.class .gitignore:3:*.class App.class
Git會告訴我們,.gitignore的第3行規則忽略了該文件,於是我們就可以知道應該修訂哪個規則。
參考文章
忽略特殊文件, 廖雪峰的官方網站
gitignore , 文件的官方定義
