# Git 操作指南 - 撤回和重新添加文件 ## ✅ 已完成的操作 ### 1. 撤回暂存区 已执行 `git reset` 命令,清空了暂存区的所有文件。 ## 📝 修改的 .gitignore 已在 `.gitignore` 中添加以下规则: ```gitignore # Project specific - 忽略知识库文档 backend/knowledge_base/documents/*.docx backend/knowledge_base/documents/*.xlsx backend/knowledge_base/documents/*.pdf backend/knowledge_base/documents/*.pptx backend/knowledge_base/documents/*.vsdx backend/knowledge_base/documents/*.xls # Temporary files - 忽略临时文件 *.tmp *.temp *.bak *.swp *~ # Archives - 忽略压缩包 *.zip *.tar.gz *.rar *.7z ``` ## 🔍 下一步操作 ### 1. 查看将要提交的文件 先查看哪些文件会被添加: ```bash # 查看所有未跟踪的文件 git status # 或者更详细地查看 git status --short ``` ### 2. 重新添加文件 确认文件列表无误后: ```bash # 方式 1: 添加所有文件(推荐) git add . # 方式 2: 逐个添加文件类型(更安全) git add backend/app/ git add backend/tests/ git add backend/*.txt backend/*.ini git add backend/.env.example git add frontend/src/ git add frontend/*.json frontend/*.js frontend/*.html git add *.md .gitignore ``` ### 3. 检查暂存区 添加后再次检查: ```bash # 查看暂存区状态 git status # 查看暂存区的文件列表 git diff --cached --name-only # 查看具体改动 git diff --cached ``` ### 4. 提交 确认无误后提交: ```bash git commit -m "feat: implement ERP AI Assistant Phase 1 - Backend: FastAPI + SQLAlchemy + Claude API + RAG - Frontend: Vue 3 + Element Plus + Pinia - Features: requirement analysis, config generation, safe execution - Security: SQL injection prevention, parameterized queries - Support: LAN access, custom API endpoint" ``` ## 🎯 建议忽略的文件 如果发现还有其他不需要提交的文件,可以在 `.gitignore` 中添加: ### 常见需要忽略的文件类型 ```gitignore # 大文件 *.dmg *.iso *.img # 编译产物 *.class *.exe *.dll *.so *.dylib # 包文件 *.jar *.war *.ear # 日志文件 logs/ *.log # 数据文件 *.csv *.dat *.out # 模型文件(如果很大的话) *.model *.pkl *.h5 *.pt *.pth ``` ### 项目特定的忽略规则 ```gitignore # 知识库向量数据库(可能很大) backend/knowledge_base/chroma_db/ # 用户上传的文档 backend/knowledge_base/documents/ # 测试覆盖率报告 htmlcov/ .coverage # 本地配置 .env.local .env.*.local ``` ## 🔧 Git 常用命令 ### 撤回操作 ```bash # 撤回所有已 add 的文件 git reset # 撤回特定文件 git reset # 撤回最近一次 commit(保留修改) git reset --soft HEAD~1 # 撤回最近一次 commit(丢弃修改,慎用) git reset --hard HEAD~1 ``` ### 查看状态 ```bash # 查看当前状态 git status # 查看简洁状态 git status -s # 查看分支信息 git branch -a # 查看提交历史 git log --oneline --graph ``` ### 删除已跟踪的文件 如果某个文件已经被 git 跟踪,想要从版本控制中移除: ```bash # 从 Git 中移除但保留本地文件 git rm --cached # 从 Git 和本地都删除 git rm # 从 Git 中移除整个文件夹 git rm -r --cached ``` ## 📋 快速检查清单 提交前检查: - [ ] 是否包含 `.env` 文件?(不应包含) - [ ] 是否包含 `venv/` 或 `node_modules/`?(不应包含) - [ ] 是否包含大文件?(不应包含) - [ ] 是否包含临时文件?(不应包含) - [ ] 是否包含日志文件?(不应包含) - [ ] 是否包含个人配置?(不应包含) ## 🚨 常见问题 ### 问题 1: 文件已经在 .gitignore 中,但仍然被跟踪 **原因**: 文件在添加 .gitignore 规则之前就已经被 git 跟踪了 **解决方案**: ```bash # 从 Git 中移除但保留本地文件 git rm --cached # 然后提交 git commit -m "chore: remove tracked file from version control" ``` ### 问题 2: 添加了不该添加的文件 **解决方案**: ```bash # 1. 撤回添加 git reset # 2. 更新 .gitignore # 3. 重新添加 git add . ``` ### 问题 3: 如何只提交特定文件 **解决方案**: ```bash # 只提交代码文件 git add *.py *.vue *.js # 只提交特定目录 git add backend/app/ frontend/src/ # 交互式添加 git add -p ``` ## 📚 相关资源 - [Git 官方文档](https://git-scm.com/doc) - [.gitignore 模板集合](https://github.com/github/gitignore) - [Git Flow 工作流](https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow) --- **更新时间**: 2026-03-21