gitignore文件的使用

在使用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 仓库中。这样,你的团队成员都会遵循相同的文件忽略规则。

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@BangBang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值