GIT项目中忽略文件

本文内容节选自“既成功又完全开源的技术书籍之一 ”-- Pro Git

一般我们总会有些文件无需纳入 Git 的管理,也不希望它们总出现在未跟踪文件列表。 通常都是些自动生成的文
件,比如日志文件,或者编译过程中创建的临时文件等。 在这种情况下,我们可以创建一个名为
.gitignore
的文件,列出要忽略的文件模式。 来看一个实际的例子:

$ cat .gitignore
*.[oa]
*~
第一行告诉 Git 忽略所有以 .o .a 结尾的文件。一般这类对象文件和存档文件都是编译过程中出现的。 第二
行告诉 Git 忽略所有以波浪符(~)结尾的文件,许多文本编辑软件(比如 Emacs)都用这样的文件名保存副
本。 此外,你可能还需要忽略 log,tmp 或者 pid 目录,以及自动生成的文档等等。 要养成一开始就设置好
.gitignore 文件的习惯,以免将来误提交这类无用的文件。
文件
.gitignore 的格式规范如下:
  • 所有空行或者以 开头的行都会被 Git 忽略。
  • 可以使用标准的 glob 模式匹配。
  • 匹配模式可以以(/)开头防止递归。
  • 匹配模式可以以(/)结尾指定目录。
  • 要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。
所谓的 glob 模式是指 shell 所使用的简化了的正则表达式。 星号( * )匹配零个或多个任意字符; [abc] 匹配
25
任何一个列在方括号中的字符(这个例子要么匹配一个 a,要么匹配一个 b,要么匹配一个 c);问号( ?
)只匹配一个任意字符;如果在方括号中使用短划线分隔两个字符,表示所有在这两个字符范围内的都可以匹配
(比如
[0-9] 表示匹配所有 0 到 9 的数字)。 使用两个星号( * ) 表示匹配任意中间目录,比如`a/**/z` 可以匹
a/z , a/b/z 或 `a/b/c/z`等。
我们再看一个 .gitignore 文件的例子:

# no .a files
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the TODO file in the current directory, not subdir/TODO
/TODO
# ignore all files in the build/ directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .pdf files in the doc/ directory
doc/**/*.pdf
GitHub 有一个十分详细的针对数十种项目及语言的 .gitignore 文件列表,你可以在
https://github.com/github/gitignore 找到它.


不同类型的Web项目有不同的.gitignore文件模板,以下为你提供常见的Java Web和Python(以Django为例)Web项目的.gitignore文件模板示例: ### Java Web项目.gitignore模板 ```plaintext # 忽略.DS_Store文件 .DS_Store # 忽略所有.log文件 *.log # 忽略.gitignore文件 .gitignore # 忽略README.md文件 README.md # 忽略folder/a/file.txt文件 folder/a/file.txt # 忽略folder/a/b1/文件夹及其所有文件 folder/a/b1/ # 忽略folder/a/b2/文件夹及其所有文件 folder/a/b2/ ``` 此模板可使.gitignore忽略除少数文件以外的所有内容,像.DS_Store和*.log文件在指定文件夹中也会被忽略[^2]。 ### Python(Django)Web项目.gitignore模板 ```plaintext # 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/ # 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 .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 Pipenv, it is generally recommended to include poetry.lock in version control. # This may cause issues when developing on multiple operating systems. #poetry.lock # pdm # Similar to Pipenv and Poetry, it is generally recommended to include pdm.lock in version control. #pdm.lock # PEP 582; used by e.g. github.com/David-OConnor/pyflow __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/ ``` 这个模板涵盖了Python项目中常见的各种可忽略文件,开发者可根据实际项目需求对其进行适当的调整和补充,以此更好地管理代码仓库,提高工作效率[^1]。 ### 注意事项 如果在做git版本控制前未创建.gitignore文件,而已做了版本控制的代码想要中途添加.gitignore文件,需要清除git缓存,操作命令如下: ```bash git rm -r --cached . # 清除缓存 git add . # 添加所有文件 git commit -m 'xxxx' # 提交更新 git push ``` 注:`xxxx` 需替换为具体的提交说明信息 [^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值