当前位置: 首页 > 工具软件 > git-imerge > 使用案例 >

【代码管理】Git 之 .gitignore

孟福
2023-12-01

【问题】

Git是应用极其广泛的代码版本管理工具。在编写代码的过程中会遇到很多问题,之前遇到过团队成员在提交代码仓库时,提交了大量的图片和视频文件以及一些编译的临时文件,导致代码仓库非常大,每次其他成员拉取的时候都要拉取一堆垃圾信息。那么如何避免该问题呢?

【解决办法】

在新建仓库的时候,建立一个 .gitignore 文件,该文件可以忽略掉那些临时文件,或者不需要跟踪的文件,也可以忽略掉一些文件夹。那么 .gitignore 的规则如何呢?

  1. .gitignore 文件的存放
    该文件可以存放在仓库的根目录下,它递归应用于整个仓库中。也可以放在子目录中,子目录中的 .gitignore 作用域在其目录。
  2. .gitignore 格式规范
  • 所有空行或者以 # 开头的行都会被 Git 忽略;
  • 可以使用标准的 glob 模式匹配,它会递归应用到整个工作区;
  • 匹配模式可以以 / 开头防止递归;
  • 匹配模式可以以 / 结尾指定目录;
  • 要忽略那些指定格式的文件或目录,可以在模式前加上 ! 取反,即该类文件或目录继续进行跟踪。

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
  1. 要养成一个好的习惯
    每次建立新仓库的时候,应及时建立 .gitignore 文件,以免将来提交无用的文件。

Git 除了利用 .gitignore 方式忽略不需要跟踪的文件外。还提供了另外一种方式 exclude, exclude 存放在 .git/info/exclude , 它与 .gitignore 不同的是, exclude 是本地需要排除的文件,不会提交到仓库中应用在别人机器上,而 .gitignore 本身可以提交到仓库中,适用于使用仓库的所有人。

.gitignore 文件可以作为占位文件,例如有些文件夹需要同步到仓库中,可以在文件夹中新建一个 .gitignore 文件,下次拉取的时候可以同步拉取了这个空目录。

  1. 如果已经提交了大文件到仓库中, 怎么办?
    思考中,下集再见

【示例】

.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/

【参考】

  1. https://www.cnblogs.com/pylemon/archive/2012/07/16/2593112.html
  2. https://github.com/github/gitignore
  3. https://blog.csdn.net/ThinkWon/article/details/101447866
 类似资料: