Git
是应用极其广泛的代码版本管理工具。在编写代码的过程中会遇到很多问题,之前遇到过团队成员在提交代码仓库时,提交了大量的图片和视频文件以及一些编译的临时文件,导致代码仓库非常大,每次其他成员拉取的时候都要拉取一堆垃圾信息。那么如何避免该问题呢?
在新建仓库的时候,建立一个 .gitignore
文件,该文件可以忽略掉那些临时文件,或者不需要跟踪的文件,也可以忽略掉一些文件夹。那么 .gitignore
的规则如何呢?
.gitignore
文件的存放.gitignore
作用域在其目录。.gitignore
格式规范#
开头的行都会被 Git
忽略;/
开头防止递归;/
结尾指定目录;!
取反,即该类文件或目录继续进行跟踪。
glob
模式是指shell
简化了正则表达式。*
匹配0个或多个任意字符,[abc]
匹配任意一个列在方括号中的字符(要么匹配a
, 要么匹配b
, 要么匹配c
)。?
只匹配一个任意字符。如果方括号中使用了短划线分割两个字符,如[0-9]
会匹配0~9
之间的数字;使用**
表示匹配任意中间目录,如a/**/z
表示匹配a/z
,a/b/z
,a/b/c/d/e/z
;
# 忽略所有的 .a 文件
*.a
# 但跟踪所有的 lib.a,即便你在前面忽略了 .a 文件
!lib.a
# 只忽略当前目录下的 TODO 文件,而不忽略 subdir/TODO
/TODO
# 忽略任何目录下名为 build 的文件夹
build/
# 忽略 doc/notes.txt,但不忽略 doc/server/arch.txt
doc/*.txt
# 忽略 doc/ 目录及其所有子目录下的 .pdf 文件
doc/**/*.pdf
.gitignore
文件,以免将来提交无用的文件。
Git
除了利用.gitignore
方式忽略不需要跟踪的文件外。还提供了另外一种方式exclude
,exclude
存放在.git/info/exclude
, 它与.gitignore
不同的是,exclude
是本地需要排除的文件,不会提交到仓库中应用在别人机器上,而.gitignore
本身可以提交到仓库中,适用于使用仓库的所有人。
.gitignore
文件可以作为占位文件,例如有些文件夹需要同步到仓库中,可以在文件夹中新建一个.gitignore
文件,下次拉取的时候可以同步拉取了这个空目录。
c++/cuda
项目下的 .gitignore
文件模板
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
# cuda
*.i
*.ii
*.gpu
*.ptx
*.cubin
*.fatbin
c
项目下的 .gitignore
文件模板
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
Python
项目下的 .gitignore
文件模板
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/