当前位置: 首页> 教育> 就业 > gitignore文件的使用

gitignore文件的使用

时间:2025/7/9 13:26:19来源:https://blog.csdn.net/weixin_38346042/article/details/141058379 浏览次数:0次

在使用GitLab进行版本控制时,如果你想要忽略一些文件或目录不被提交(比如生成的权重、预测的图片等),你可以在项目的根目录下创建或编辑一个.gitignore文件。在这个文件中,你可以列出那些你希望Git忽略的文件和目录的模式。

1. 基本语法

  • 每条规则占一行
  • 空白行或以 # 开头的行:这些行将被忽略。
  • 标准 glob 模式(通配符):可以包含 *(匹配任意数量字符)、?(匹配单个字符)、[abc](匹配方括号内的任意一个字符)。
  • 目录:如果要忽略目录,需要在模式后面加上 /
  • 否定规则:在模式前加上 ! 来包含那些被之前规则排除的文件

2. 使用说明

(1) 忽略所有.jpg文件:

*.jpg

(2) 忽略 build文件夹

build/      		#忽略目录要加'/'

(3) 忽略 build/ 目录,但包含 build/src/ 目录

build/     			# 忽略build目录
!build/src/		    #  保留 build/src/ 目录,模式前`加上 ! `来包含那些`被之前规则排除的文件`

(4) 忽略temp/目录和所有.tmp文件:

temp/
*.tmp

(5) output 和 output/ 的区别

  • output:这种写法会忽略名为 output 的文件,无论它位于项目的哪个位置。它不会忽略任何名为 output 的目录
  • output/:这种写法会忽略 output/ 这个目录下的所有内容,包括子目录和文件。它不忽略其他目录或文件名为 output 的文件

简单来说,使用不带斜杠 / 的 output 仅忽略文件,而带斜杠的 output/ 忽略整个目录及其内容

3. 案例

项目的根目录下创建或编辑一个.gitignore文件, 对应的内容如下:

### Linux ###
*~# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*# KDE directory preferences
.directory# Linux trash folder which might appear on any partition or disk
.Trash-*# .nfs files are created when an open file is removed but is still being accessed
.nfs*### PyCharm ###
# User-specific stuff
.idea# CMake
cmake-build-*/# Mongo Explorer plugin
.idea/**/mongoSettings.xml# File-based project format
*.iws# IntelliJ
out/# mpeltonen/sbt-idea plugin
.idea_modules/# JIRA plugin
atlassian-ide-plugin.xml# Cursive Clojure plugin
.idea/replstate.xml# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties# Editor-based Rest Client
.idea/httpRequests# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser# JetBrains templates
**___jb_tmp___### Python ###
# 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/
pip-wheel-metadata/
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
.hypothesis/
.pytest_cache/# Translations
*.mo
*.pot# Django stuff:
*.log
local_settings.py
db.sqlite3# Flask stuff:
instance/
.webassets-cache# Scrapy stuff:
.scrapy# Sphinx documentation
docs/_build/
docs/build/# PyBuilder
target/# Jupyter Notebook
.ipynb_checkpoints# IPython
profile_default/
ipython_config.py# pyenv
.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# celery beat schedule file
celerybeat-schedule# 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/### Vim ###
# Swap
[._]*.s[a-v][a-z]
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]# Session
Session.vim# Temporary
.netrwhist
# Auto-generated tag files
tags
# Persistent undo
[._]*.un~### Researcher ###
# output
train_log
docs/api
.code-workspace.code-workspace
output                      		# 忽略 output 文件
outputs/						    # 忽略outputs目录
instant_test_output
inference_test_output
*.pkl
*.npy
*.pth
*.bin
*.pickle							# 忽略所有.pickle文件
events.out.tfevents*# vscode
*.code-workspace
.vscode/       			 			# 忽略 .vscode目录# vim
.vim/onnx_models/     					# 忽略onnx_models目录
*.onnx			 					# 忽略所有的.onnx文件

注意事项

  • .gitignore 文件本身需要被添加到版本控制系统中,这样其他开发者才能使用相同的忽略规则。
  • 如果你之前错误地添加了某些文件到版本控制,并且想要从Git的跟踪中移除它们,但保留在本地,可以使用以下命令:
git rm --cached <file>
  • .gitignore 文件的规则是递归的,对所有子目录有效

创建 .gitignore 文件后,你可以使用文本编辑器编辑它,然后将其添加到你的 Git 仓库中。这样,你的团队成员都会遵循相同的文件忽略规则。

关键字:gitignore文件的使用

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: