編寫 .gitignore 文件


編寫 .gitignore 文件

參考文檔

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

# 2.2 Git 基礎 - 記錄每次更新到倉庫
https://git-scm.com/book/zh/v2/Git-%E5%9F%BA%E7%A1%80-%E8%AE%B0%E5%BD%95%E6%AF%8F%E6%AC%A1%E6%9B%B4%E6%96%B0%E5%88%B0%E4%BB%93%E5%BA%93

# github 提供的文件樣例
https://github.com/github/gitignore

文件示例

.idea 
*.iml
.log/
/order
/customer/
.gitignore

.idea 忽略 .gitignore 所在目錄下所有路徑中的 .idea 文件和文件夾

*.iml 忽略 .gitignore 所在目錄下所有路徑中的 .iml 為后綴的文件和文件夾

.log/ 忽略 .gitignore 所在目錄下所有路徑中的 .log 文件夾

/order 忽略 .gitignore 所在目錄下的 order 文件和文件夾

/customer/ 忽略 .gitignore 所在目錄下的 customer 文件夾

.gitignore 可以忽略自己

忽略文件

一般我們總會有些文件無需納入 Git 的管理,也不希望它們總出現在未跟蹤文件列表。 通常都是些自動生成的文件,比如日志文件,或者編譯過程中創建的臨時文件等。 在這種情況下,我們可以創建一個名為 .gitignore 的文件,列出要忽略的文件模式。 來看一個實際的例子:

$ cat .gitignore
*.[oa]
*~

第一行告訴 Git 忽略所有以 .o.a 結尾的文件。一般這類對象文件和存檔文件都是編譯過程中出現的。 第二行告訴 Git 忽略所有以波浪符(~)結尾的文件,許多文本編輯軟件(比如 Emacs)都用這樣的文件名保存副本。 此外,你可能還需要忽略 log,tmp 或者 pid 目錄,以及自動生成的文檔等等。 要養成一開始就設置好 .gitignore 文件的習慣,以免將來誤提交這類無用的文件。

文件 .gitignore 的格式規范如下:

  • 所有空行或者以 開頭的行都會被 Git 忽略。
  • 可以使用標准的 glob 模式匹配。
  • 匹配模式可以以(/)開頭防止遞歸。
  • 匹配模式可以以(/)結尾指定目錄。
  • 要忽略指定模式以外的文件或目錄,可以在模式前加上驚嘆號(!)取反。

所謂的 glob 模式是指 shell 所使用的簡化了的正則表達式。 星號(*)匹配零個或多個任意字符;[abc] 匹配任何一個列在方括號中的字符(這個例子要么匹配一個 a,要么匹配一個 b,要么匹配一個 c);問號(?)只匹配一個任意字符;如果在方括號中使用短划線分隔兩個字符,表示所有在這兩個字符范圍內的都可以匹配(比如 [0-9] 表示匹配所有 0 到 9 的數字)。 使用兩個星號(*) 表示匹配任意中間目錄,比如 a/**/z 可以匹配 a/z , a/b/za/b/c/z 等。

我們再看一個 .gitignore 文件的例子:

# no .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in the build/ directory
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory
doc/**/*.pdf

GitHub 有一個十分詳細的針對數十種項目及語言的 .gitignore 文件列表,你可以在 https://github.com/github/gitignore 找到它.

強制添加被忽略的文件

添加后這個文件的修改,刪除都會被管理。

1@DESKTOP-3H9092J MINGW64 /e/x1/x1/demo1 (master)
$ git add .log/1.txt
The following paths are ignored by one of your .gitignore files:
.log/1.txt
Use -f if you really want to add them.

1@DESKTOP-3H9092J MINGW64 /e/x1/x1/demo1 (master)
$ git add -f .log/1.txt

1@DESKTOP-3H9092J MINGW64 /e/x1/x1/demo1 (master)
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   .log/1.txt

1@DESKTOP-3H9092J MINGW64 /e/x1/x1/demo1 (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   .log/1.txt

1@DESKTOP-3H9092J MINGW64 /e/x1/x1/demo1 (master)
$ git add .log/1.txt

1@DESKTOP-3H9092J MINGW64 /e/x1/x1/demo1 (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   .log/1.txt
        
1@DESKTOP-3H9092J MINGW64 /e/x1/x1/demo1 (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    .log/1.txt

1@DESKTOP-3H9092J MINGW64 /e/x1/x1/demo1 (master)
$ git add .log/1.txt

1@DESKTOP-3H9092J MINGW64 /e/x1/x1/demo1 (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    .log/1.txt

讓原本被管理的文件被忽略

在 .gitignore 中忽略原本被管理的 *.iml 文件。然后刪除被管理的 *.iml 文件。提交這次刪除。那么下次如果

idea再次生成了 *.iml 文件,這時就會被忽略了。如果你想強制添加,則可以使用上面的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM