# 第 19 天：设定 .gitignore 忽略清单

在开发项目时，工作目录下可能经常会有新的文件产生 (可能是通过 Visual Studio 工具产生的那些暂存文件或快照文件)，可能有许多文件并不需要列入版本控制，所以必须要排除这些文件，我们称为「忽略清单」。

## 关于 .gitignore 文件

在 Git 里面，是通过 `.gitignore` 文件来进行定义「忽略清单」，主要的目的是排除一些不需要加入版控的文件，列在里面的档名或路径 (可包含万用字元)，都不会出现在 `git status` 的结果中，自然也不会在执行 `git add` 的时候被加入。不过，这仅限于 `Untracked file` 而已，那些已经列入版控的文件 (`Staged file`)，不受 `.gitignore` 文件控制。

## 通过 GitHub 建立预设的忽略清单

如果你曾经在 GitHub 建立过项目，可能会用过这个功能，就在建立新的仓库(Repository)时，可以让你选择 GitHub 预先帮你定义好的忽略清单，这个忽略清单其实就只是一个文件而已，其档名为 `.gitignore`，并预设置于项目跟目录下。

![image](https://1991450022-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fj2h2C95aFkfa08k9hVd3%2Fuploads%2Fgit-blob-5b3908a988bb996572f285b689e4944e7a70f6d4%2F01.png?alt=media)

我们以上图这个 CSharp 项目为例，建立完成后，在仓库中就会出现一个预设的 `.gitignore` 文件：

![image](https://1991450022-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fj2h2C95aFkfa08k9hVd3%2Fuploads%2Fgit-blob-7d1b31a9964c1aa6653ee04e6bdf0d8d6d147e60%2F02.png?alt=media)

我们可以看看其内容：<https://github.com/doggy8088/sandbox-csharp/blob/master/.gitignore>

```
# Build Folders (you can keep bin if you'd like, to store dlls and pdbs)
[Bb]in/
[Oo]bj/

# mstest test results
TestResults

## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

# User-specific files
*.suo
*.user
*.sln.docstates

# Build results
[Dd]ebug/
[Rr]elease/
x64/
*_i.c
*_p.c
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.log
*.vspscc
*.vssscc
.builds

# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf

# Visual Studio profiler
*.psess
*.vsp
*.vspx

# Guidance Automation Toolkit
*.gpState

# ReSharper is a .NET coding add-in
_ReSharper*

# NCrunch
*.ncrunch*
.*crunch*.local.xml

# Installshield output folder 
[Ee]xpress

# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html

# Click-Once directory
publish

# Publish Web Output
*.Publish.xml

# NuGet Packages Directory
packages

# Windows Azure Build Output
csx
*.build.csdef

# Windows Store app package directory
AppPackages/

# Others
[Bb]in
[Oo]bj
sql
TestResults
[Tt]est[Rr]esult*
*.Cache
ClientBin
[Ss]tyle[Cc]op.*
~$*
*.dbmdl
Generated_Code #added for RIA/Silverlight projects

# Backup & report files from converting an old project file to a newer
# Visual Studio version. Backup files are not needed, because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
```

这些内容，真的就是在用 Visual Studio 写 CSharp 项目时常见的「忽略清单」，非常具有实用价值，如果各位还有一些额外的文件名称或路径要加入，也可以自行添加在这个文件里面。

## 参考其他程序语言的 `.gitignore` 内容范本

在 GitHub 上面，事实上还提供了很多其他开发环境所需的 `.gitignore` 范本，也都非常值得参考：

![image](https://1991450022-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fj2h2C95aFkfa08k9hVd3%2Fuploads%2Fgit-blob-90579cf2821fc048a6bf97992bb603e7e8efc52a%2F03.png?alt=media)

## 今日小结

今天的 `.gitignore` 文件，我们几乎每个项目都会用到，算是使用 Git 时一个必备的重要文件。

## 参考连结

* [gitignore(5) Manual Page](http://git-scm.com/docs/gitignore)
* [请勿将某些文件类型的文件签入到 Subversion 版本库 (二版)](http://blog.miniasp.com/post/2012/03/30/Do-not-commit-these-file-type-into-subversion-repository-2.aspx)

***

* [HOME](https://kerryhuangs-organization.gitbook.io/kerry-de-bi-ji-ben/git/30-tian-jing-tong-git-ban-ben-kong-guan)
* [回目录](https://kerryhuangs-organization.gitbook.io/kerry-de-bi-ji-ben/git/30-tian-jing-tong-git-ban-ben-kong-guan/zh-cn)
* [前一天：修正 commit 过的版本历史记录 Part 1 (reset & amend)](https://kerryhuangs-organization.gitbook.io/kerry-de-bi-ji-ben/git/30-tian-jing-tong-git-ban-ben-kong-guan/zh-cn/18)
* [下一天：修正 commit 过的版本历史记录 Part 2 (revert)](https://kerryhuangs-organization.gitbook.io/kerry-de-bi-ji-ben/git/30-tian-jing-tong-git-ban-ben-kong-guan/zh-cn/20)

***
