一般來說每個Git項目中都需要一個“.gitignore”檔案,這個檔案的作用就是告訴Git哪些檔案不需要添加到版本管理中。
實際項目中,很多檔案都是不需要版本管理的,比如Python的.pyc檔案和一些包含密碼的配置檔案等等。
這個檔案的内容是一些規則,Git會根據這些規則來判斷是否将檔案添加到版本控制中。
下面我們看看常用的規則:
- /mtk/ 過濾整個檔案夾
- *.zip 過濾所有.zip檔案
- /mtk/do.c 過濾某個具體檔案
很簡單吧,被過濾掉的檔案就不會出現在你的GitHub庫中了,當然本地庫中還有,隻是push的時候不會上傳。
需要注意的是,gitignore還可以指定要将哪些檔案添加到版本管理中:
- !*.zip
- !/mtk/one.txt
唯一的差別就是規則開頭多了一個感歎号,Git會将滿足這類規則的檔案添加到版本管理中。
為什麼要有兩種規則呢?想象一個場景:我們隻需要管理/mtk/目錄中的one.txt檔案,這個目錄中的其他檔案都不需要管理。那麼我們就需要使用:
- /mtk/
- !/mtk/one.txt
推薦配置如下:
## .gitignore for Grails 1.2 and 1.3
# .gitignore for maven
target/
*.releaseBackup
# web application files
#/web-app/WEB-INF
# IDE support files
/.classpath
/.launch
/.project
/.settings
/*.launch
/*.tmproj
/ivy*
/eclipse
# default HSQL database files for production mode
/prodDb.*
# general HSQL database files
*Db.properties
*Db.script
# logs
/stacktrace.log
/test/reports
/logs
*.log
*.log.*
# project release file
/*.war
# plugin release file
/*.zip
/*.zip.sha1
# older plugin install locations
/plugins
/web-app/plugins
/web-app/WEB-INF/classes
# "temporary" build files
target/
out/
build/
# other
*.iws
#.gitignore for java
*.class
# Package Files #
*.jar
*.war
*.ear
## .gitignore for eclipse
*.pydevproject
.project
.metadata
bin/**
tmp/**
tmp/**/*
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath
# External tool builders
.externalToolBuilders/
# Locally stored "Eclipse launch configurations"
*.launch
# CDT-specific
.cproject
# PDT-specific
.buildpath
## .gitignore for intellij
*.iml
*.ipr
*.iws
.idea/
## .gitignore for linux
.*
!.gitignore
*~
## .gitignore for windows
# Windows image file caches
Thumbs.db
ehthumbs.db
# Folder config file
Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
## .gitignore for mac os x
.DS_Store
.AppleDouble
.LSOverride
Icon
# Thumbnails
._*
# Files that might appear on external disk
.Spotlight-V100
.Trashes
## hack for graddle wrapper
!wrapper/*.jar
!**/wrapper/*.jar
參考自:https://www.javastack.cn/article/2018/git-common-gitignore-file/