Git整合
Git版本控制是現代軟體開發的基礎。Claude Code與Git深度整合,不僅能執行常見的Git操作,還能理解你的程式碼變更意圖,自動生成規範的提交資訊,智慧處理程式碼審查,甚至協助建立和管理Pull Request。
這篇文章介紹Claude Code的Git整合能力,幫你建立高效的版本控制工作流。
Git狀態檢視
實時狀態監控
Claude Code會自動跟蹤Git倉庫狀態變化:
查看当前Git状态
Claude Code會提供清晰的狀態報告:
📊 Git Repository Status
Branch: main (ahead of origin/main by 2 commits)
📝 Staged Changes (3 files):
✓ src/components/Button.jsx (Modified)
✓ src/utils/helpers.js (Modified)
✓ tests/Button.test.js (Modified)
📄 Unstaged Changes (2 files):
~ src/App.js (Modified)
~ src/config.js (Modified)
❓ Untracked Files (4 files):
? src/components/NewFeature.jsx
? src/styles/newtheme.css
? .env.local
? docs/notes.md
Recent commits:
a3b2c1d (2 hours ago) Add dark mode support
f9e8d7c (5 hours ago) Fix authentication bug
e6a5b4c (1 day ago) Update dependencies
Would you like me to:
1. Review the staged changes
2. Commit the staged changes
3. Stage all changes
4. Create a new branch
智慧變更分析
Claude Code不只列出檔案,還能分析變更內容:
分析一下当前所有的改动
🔍 Change Analysis
Summary: 9 files changed across 3 categories
1. Feature Implementation (2 files):
✓ NewFeature.jsx - Complete new component (245 lines)
✓ newtheme.css - Dark theme styles (89 lines)
2. Bug Fixes (2 files):
✓ Button.jsx - Fixed hover state bug (3 lines changed)
✓ helpers.js - Fixed date formatting (5 lines changed)
3. Refactoring (3 files):
✓ App.js - Improved component structure (23 lines changed)
✓ config.js - Centralized configuration (15 lines changed)
✓ Button.test.js - Added missing tests (34 lines added)
Risk Level: 🟢 Low
- No breaking changes detected
- All changes are backward compatible
- Test coverage: 95%
Recommendations:
✓ Safe to commit together
💡 Consider splitting into 2 commits:
1. Bug fixes (can be deployed quickly)
2. New features (can be tested separately)
對比傳統方式
傳統方式:
git status
# 输出难以理解,需要自己判断文件状态
# 不知道具体改了什么
# 无法判断变更的影响范围
Claude Code方式:
这些改动会影响哪些功能?
Claude Code會:
- 分析程式碼變更的影響
- 識別可能的問題
- 建議測試策略
- 評估合併風險
智慧提交(Smart Commit)
自動生成提交資訊
這是Claude Code最強大的功能之一:
帮我提交当前的更改
Claude Code會:
- 並行執行Git命令收集資訊
- 分析所有變更
- 生成規範的提交資訊
I'll create a commit with your changes.
📊 Analyzing changes...
✓ Found 5 modified files
✓ Analyzed 127 lines changed
✓ Identified 3 feature additions
✏️ Drafting commit message...
Subject: Add user authentication feature
Body:
- Implement login/logout functionality with JWT tokens
- Add protected route middleware for authenticated pages
- Create user profile management component
- Update API integration with authentication endpoints
- Add comprehensive unit tests for auth flow
Technical Details:
- Frontend: Added AuthContext and useAuth hook
- Backend: Integrated JWT token refresh mechanism
- Security: Implemented token storage in httpOnly cookies
- Testing: Added 12 new test cases (98% coverage)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude (noreply@anthropic.com)
✓ Commit created successfully: a3b2c1d
自定義提交資訊風格
你可以指定提交資訊的格式:
使用Conventional Commits格式提交
feat(auth): implement user authentication flow
- add login and logout functionality
- implement JWT token management
- create protected route middleware
- add user profile component
BREAKING CHANGE: requires API v2.0+
Co-Authored-By: Claude (noreply@anthropic.com)
✓ Commit created: f8e7d6c
分階段提交
對於複雜的更改,可以分批提交:
先把bug修复提交了,新功能稍后再提交
Claude Code會智慧分類:
📊 Separating changes...
Bug Fixes (staging):
✓ src/utils/date.js - Fixed timezone issue
✓ src/components/Button.jsx - Fixed hover bug
Committing bug fixes...
fix: resolve timezone and button hover bugs
- Fix date parsing timezone offset issue
- Fix button hover state not updating correctly
✓ Committed: a1b2c3d
Remaining changes (not committed):
~ src/features/Auth.jsx - New authentication feature
~ src/features/Profile.jsx - New profile page
Ready to commit features when you're ready
互動式提交
Claude Code支援更細粒度的控制:
帮 我提交更改,但.env.local不要提交
✓ Staging files...
✓ src/App.js
✓ src/components/*.jsx
✓ src/utils/*.js
⚠️ Excluded from commit:
🔒 .env.local (contains sensitive data)
✓ Commit created: b2c3d4e
💡 Suggestion:
Add .env.local to .gitignore if you haven't already