Learn Git in 30 days——第 19 天:設定 .gitignore 忽略清單

寫的非常好的一個Git系列文章,強烈推薦

原文鏈接:https://github.com/doggy8088/Learn-Git-in-30-days/tree/master/zh-cn

在開發項目時,工作目錄下可能經常會有新的文件產生 (可能是通過 Visual Studio 工具產生的那些暫存文件或快照文件),可能有許多文件並不需要列入版本控制,所以必須要排除這些文件,我們稱爲「忽略清單」。

關於 .gitignore 文件

在 Git 裏面,是通過 .gitignore 文件來進行定義「忽略清單」,主要的目的是排除一些不需要加入版控的文件,列在裏面的檔名或路徑 (可包含萬用字元),都不會出現在 git status 的結果中,自然也不會在執行 git add 的時候被加入。不過,這僅限於 Untracked file 而已,那些已經列入版控的文件 (Staged file),不受 .gitignore 文件控制。

通過 GitHub 建立預設的忽略清單

如果你曾經在 GitHub 建立過項目,可能會用過這個功能,就在建立新的倉庫(Repository)時,可以讓你選擇 GitHub 預先幫你定義好的忽略清單,這個忽略清單其實就只是一個文件而已,其檔名爲 .gitignore,並預設置於項目跟目錄下。

image

我們以上圖這個 CSharp 項目爲例,建立完成後,在倉庫中就會出現一個預設的 .gitignore 文件:

image

我們可以看看其內容: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

今日小結

今天的 .gitignore 文件,我們幾乎每個項目都會用到,算是使用 Git 時一個必備的重要文件。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章