原文网址:git--.gitignore--使用/详解/实例_IT利刃出鞘的博客-CSDN博客
本文介绍git的.gitignore忽略文件的用法。
项目中并不是所有文件都需要保存到版本库中的,例如“target”目录及目录下的文件就可以忽略。
忽略某个文件(不提交到版本库的方法):在Git工作区的根目录下创建一个.gitignore文件,然后把要忽略的文件名填进去,Git就会自动忽略这些文件或目录。
如下文件建议忽略(不上传到版本仓库):
Git有多个地方可以配置忽略规则,它的优先级如下(由高到低):
采用的匹配规则是:globs。
.gitignore只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。
解决方法:
git rm -r --cached .
git add .
git commit -m .gitignore
git push origin master
如果发现.gitignore有问题,需要找出来到底哪个规则写错了,可以用git check-ignore命令检查:
$ git check-ignore -v HelloWorld.class
.gitignore:1:*.class HelloWorld.class
可以看到HelloWorld.class匹配到了我们的第一条*.class的忽略规则所以文件被忽略了。
### maven ###
**/.mvn/
**/target/
### gradle ###
**/.gradle/
**/build/
### IntelliJ IDEA ###
**/.idea/
**/*.iws
**/*.iml
**/*.ipr
### VS Code ###
.vscode/
### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
.DS_Store
node_modules
/dist
# local env files
.env.local
.env.*.local
# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
.idea/
# dotenv
.env
# virtualenv
.venv
venv/
ENV/
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg