目录

Sourcetree使用.gitignore忽略文件或者文件夹

Sourcetree——使用.gitignore忽略文件或者文件夹

一、为何需要文件忽略机制?

1.1 为什么要会略?

对于开发者而言,明智地选择忽略某些文件类型,能带来三大核心优势:

  • 仓库纯净性 :避免二进制文件、编译产物等污染代码库
  • 安全防护 :防止敏感信息(如API密钥、数据库凭证)意外泄露
  • 效率提升 :减少无意义的版本追踪,加速克隆和拉取操作

1.2 通常忽略那些文件?

典型需忽略文件类型

文件类别常见示例
系统生成文件.DS_StoreThumbs.dbDesktop.ini
编译产物*.class (Java)、 *.o (C++)、 *.pyc (Python)、 /dist/ 目录
依赖管理目录node_modules/vendor/target/
开发环境文件.idea/.vscode/.env
日志与临时文件*.log*.tmpnpm-debug.log*

示例:

我只修改了.cmd文件,但提交修改的时候,有很多编译产物

https://i-blog.csdnimg.cn/direct/58e915ed767a4480bf063f27693c3d72.png

二、.gitignore配置深度解析

2.1 文件作用域与优先级

层级生效机制(从上到下优先级递减)

  1. 项目根目录/.gitignore         # 作用于整个项目

  2. 子目录/.gitignore                # 作用于该目录及其子目录

  3. $GIT_DIR/info/exclude      # 本地仓库级配置

  4. ~/.gitignore_global             # 全局配置(需执行git config –global core.excludesfile ~/.gitignore_global)

2.2 高级语法规则

基础匹配

*.log                    # 所有.log文件

!error.log             # 排除规则(保留error.log)

目录匹配

build/                  # 忽略所有build目录

doc/*.txt             # 忽略doc目录下txt文件(不递归子目录)

通配符扩展

temp?                # 匹配temp后接一个字符的文件(如tempa、temp1)

config.[tj]s          # 匹配config.ts和config.js

注释与范围

[0-9].csv             # 匹配数字开头的csv文件

重要:不要提交密钥文件

secret.key

.gitignore 的规则是 从上到下逐行匹配 的,后面的规则可以覆盖前面的规则。

否定规则( ! )是唯一可以覆盖前面忽略规则的方式。

三、Sourcetree的可视化设置

3. 1首次提交.gitignore

3.1.1点击设置

https://i-blog.csdnimg.cn/direct/87ab95839b634b02b1ec231c892584cd.png

3.1.2在仓库设置中选择高级,点击编辑。

https://i-blog.csdnimg.cn/direct/faddd9642c1b4412b12e498f2300aefd.png

3.3.3在gitignore中添加需要忽略的文件。

可以使用 生成模板

以下是一个常用的.gitignore文件模板,可根据个人项目需求,按照.gitignore配置的语法规则2.2,进行增删改查。

# macOS system files
.DS_Store
.AppleDouble
.LSOverride
Icon
._*

# IDE - IntelliJ IDEA
.idea/
*.iml
*.iws
*.ipr
out/
.idea_modules/

# IDE - VSCode
.vscode/
*.code-workspace

# Obsidian files
.obsidian/

# Compiled files
*.class
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar

# Logs and databases
*.log
*.sqlite
*.db

# Node
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Python
__pycache__/
*.py[cod]
*$py.class
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# Temporary files
*.swp
*.swo
*~

然后重新提交就好啦,这里.gitignore文件也要提交。

一定注意.gitignore文件要提交成功下次才能忽略设置的文件或目录!

3.2对于已经被git追踪的文件

如果是第一次提交,就会发现忽略文件已经没有了,不是第一次的这个时候你要是提交,就会发现你想要忽略的文件要是有改动,依然让你提交,这是因为这些文件已经加入git的版本控制库了,仅仅添加.gitignore文件是不够的,需要先清除缓存才行。

3.2.1清除Git缓存:

# 删除Git缓存(不会删除实际文件)
git rm -r --cached .

https://i-blog.csdnimg.cn/direct/05b00b1fce3e423e8e72e109d2b2512d.png

3.2.2 添加新的.gitignore文件,提交更改

# 添加.gitignore文件到Git
git add .gitignore

# 添加其他文件
git add .

# 提交更改
git commit -m "Add .gitignore file and remove ignored files from git"

https://i-blog.csdnimg.cn/direct/6b137b408aaf469288e229ef8e9c1163.png

3.3.3 推送到远程仓库

# 推送到远程仓库
git push origin master  # 或者其他分支名

https://i-blog.csdnimg.cn/direct/5c5d4a45a0fd4d6fa37b0caba1aee0ae.png

示例问题解决:(这里我尝试修改了其他文件)

https://i-blog.csdnimg.cn/direct/e0b2eecd21b94609943dd86a859dfc88.png

参考连接: