Git入門之.gitignore

語言: CN / TW / HK

theme: hydrogen

本文已參與掘金創作者訓練營第三期「話題寫作」賽道,詳情查看:掘力計劃|創作者訓練營第三期正在進行,「寫」出個人影響力

前言

在進行本地開發的時候,會經常有意無意的產生一些文件,這些文件我們在希望進行文件管理的時候,會被忽略掉。有些同學這個時候就開始瘋狂delete文件了。其實git為我們提供了忽略文件的方法-.gitignore

.gitignore

.gitignore是一個文件,名稱就是 .gitignore,一般放在項目的根目錄,與.git 平級。

這個文件是用來指定哪些文件不被納入git管理的。git commit 不會提交這些文件。

這個文件不是自動生成的,需要你手動創建並編寫規則。

一些常見的例子: 1. vscode自動創建的.vscode文件 2. 前端安裝依賴生成的巨大的node_modules文件夾 3. Electron打包生成的build文件夾 4. IDE 自動生成的.idea文件 5. 一些不想上傳的文件,例如密碼配置文件之類。 6. 。。。。。。 當然這些例子是特別多的。

例子

下面這個是一個常見的前端開發的.gitignore 文件。

``` .DS_Store node_modules/ dist/ CONTRIBUTING.md npm-debug.log yarn-debug.log yarn-error.log yarn.lock package-lock.json test/unit/coverage test/e2e/reports selenium-debug.log

Editor directories and files

.idea .vscode .suo .ntvs .njsproj *.sln

```

匹配規則

| 表達式 | 匹配文件 | 説明* | | --- | --- | --- | | **/testIgnore | testIgnore/debug.log build/testIgnore/debug.log | 表示當前項目下的任何子目錄為testIgnore的目錄 | | **/testIgnore/debug.log | testIgnore/debug.log build/testIgnore/debug.log | 表示當前項目下的任何子目錄為testIgnore並且testIgnore下有debug.log的目錄 | | *.log | debug.log foo.log .log testIgnore/debug.log | * 匹配零個或多個字符的通配符。 | | *.log !other.log | debug.log build.log 但這個文件不能是other.log 或者子目錄下的 testIgnore/other.log | !表示排除這個文件 | | *.log !other/*.log | debug.log 但不是 other目錄下的任何.log 文件 | !表示排除這個文件 * 表示任意文件名| | /debug.log | debug.log 但不是 testIgnore/debug.log | /表示根目錄。 | | debug.log | debug.log testIgnore/debug.log | 匹配任何文件下的debug文件 | | debug?.log | debug0.log debugg.log 但不是 debug123.log | 問號匹配一個字符。 | | debug[0-9].log | debug0.log debug1.log 但不是 debug10.log | 方括號用於匹配指定範圍內的單個字符。 | | debug[01].log | debug0.log debug1.log 但不是 debug2.log debug01.log | 方括號匹配指定集的單個字符。 | | debug[!01].log | debug2.log 但不是 debug0.log debug1.log debug01.log | 感歎號可用於匹配除指定集中的字符之外的任何字符。 | | debug[a-z].log | debuga.log debugb.log 但不是 debug1.log | 範圍可以是字母。 | | testIgnore | testIgnore testIgnore/debug.log testIgnore/latest/foo.bar build/testIgnore build/testIgnore/debug.log | 匹配名為testIgnore的文件,匹配所有testIgnore目錄以及目錄下的所有目錄以及文件| | testIgnore/ | testIgnore/debug.log testIgnore/latest/foo.bar build/testIgnore/foo.bar build/testIgnore/latest/debug.log | 匹配所有testIgnore目錄以及目錄下的所有目錄以及文件,這個和上面的區別就是testIgnore只能是目錄 | | testIgnore/**/debug.log | testIgnore/debug.log testIgnore/file1/debug.log testIgnore/file1/file2/debug.log | *匹配零個或多個目錄。 | | testIgnore/*1/debug.log | testIgnore/file1/debug.log testIgnore/file21/debug.log 但不是 testIgnore/latest/debug.log | 匹配以1結尾的文件夾。 | | testIgnore/debug.log | testIgnore/debug.log | 只匹配testIgnore/debug.log |

註釋

使用 # 在.gitignore文件中包含註釋:

在上面的例子中,其實我們已經提到了: ```

Editor directories and files

```

在根目錄定義.gitignore

我們通常是在項目的根目錄中定義.gitignore文件,但是你也可以選擇再不同的文件夾下定義不同的.gitignore文件。但是我不建議這麼做,這樣的管理會使得你自己混亂,同時也不利於團隊的項目開發。

再提交 忽略已經納入管理的文件

有時候,由於失誤或者考慮不到位,我們會將本不應該的文件納入git管理,這個時候我們該如何補救?

``` $ echo debug.log >> .gitignore

$ git rm --cached debug.log

$ git commit -m "Start ignoring debug.log"

```

再提交 將已忽略文件納入管理

``` $ echo !debug.log >> .gitignore $ cat .gitignore

$ git add -f debug.log

$ git commit -m "強制提交" ```