| name | git-workflow |
| description | Git workflow management with atomic commit principles. Capabilities: commit organization, branching strategies, merge/rebase workflows, PR management, history cleanup, staged change analysis, single-responsibility commits. Actions: commit, push, pull, merge, rebase, branch, stage, stash git operations. Keywords: git commit, git push, git pull, git merge, git rebase, git branch, git stash, atomic commit, commit message, conventional commits, branching strategy, GitFlow, trunk-based, PR, pull request, code review, git history, cherry-pick, squash, amend, interactive rebase, staged changes. Use when: organizing commits, creating branches, merging code, rebasing, writing commit messages, managing PRs, cleaning git history, analyzing staged changes. |
Git Workflow & Best Practices
Purpose
Comprehensive guide for git operations with emphasis on clean history, atomic commits, and professional workflows. Automatically analyzes staged changes and enforces single-responsibility principle.
When to Use
Activate for any git operation:
- Committing changes (especially multiple files)
- Creating branches
- Merging or rebasing
- Managing git history
- Writing commit messages
- Organizing staging area
- Code review preparation
- Repository management
Core Philosophy
Single Responsibility Rule ⭐
CRITICAL: Before committing, analyze staged changes and divide into atomic commits.
Process:
- Run
git status to see all staged files
- Identify different concerns/features
- Unstage everything:
git reset HEAD
- Stage files by concern, one group at a time
- Commit each group with focused message
- Repeat until all changes are committed
Why: Makes history reviewable, revertable, and maintainable.
⛔ MANDATORY Gate Before Commit
Ask yourself: "If I need to revert ONLY ONE of these changes tomorrow, can I?"
- NO → You have multiple concerns → MUST split into separate commits
- YES → Proceed with single commit
Common trap: "All files are related to the same feature request" is NOT a valid reason to bundle. Each independently revertable change = separate commit.
Commit Organization
Analyzing Staged Changes
git status
git diff --cached --stat
git diff --cached
git diff --cached path/to/file
Grouping Strategies
By Feature:
- Auth system changes → one commit
- Payment module → separate commit
- User profile → another commit
By Layer:
- Database migrations → first commit
- Backend API → second commit
- Frontend UI → third commit
- Tests → fourth commit
By Type:
- New features (feat)
- Bug fixes (fix)
- Refactoring (refactor)
- Documentation (docs)
- Performance (perf)
- Tests (test)
By Dependency:
- Foundation/infrastructure first
- Features that depend on foundation second
Division Workflow
git status
git diff --cached --stat
git reset HEAD
git add file1.ts file2.ts directory/
git diff --cached --stat
git commit -m "type: concise description"
Example: Real Scenario
Situation: 29 files staged with mixed concerns
$ git status
Changes to be committed:
modified: src/app/styles/page.tsx
new file: src/core/domain/models/TradingStyle.ts
new file: src/infrastructure/database/migrations/create_trading_styles.ts
...
modified: src/app/history/page.tsx
modified: src/app/api/history/recommendations/route.ts
...
Solution:
git reset HEAD
git add \
package.json pnpm-lock.yaml \
src/app/styles/ \
src/core/domain/models/TradingStyle.ts \
src/core/ports/ITradingStyleRepository.ts \
src/infrastructure/database/TradingStyleRepository.ts \
src/infrastructure/database/migrations/create_trading_styles.ts
git commit -m "feat: Add trading style persona system for AI-powered analysis"
git add \
src/app/history/page.tsx \
src/app/api/history/recommendations/route.ts \
src/infrastructure/database/TimeseriesRepository.ts \
src/components/layout/AppLayout.tsx
git commit -m "feat: Add comprehensive search and filtering to history page"
Result: Clean, focused commits that are independently reviewable and revertable.
Commit Messages
Conventional Commits Format
<type>(<scope>): <subject>
<body>
<footer>
Types
feat - New feature
fix - Bug fix
refactor - Code restructuring (no behavior change)
perf - Performance improvement
docs - Documentation only
style - Formatting, whitespace, semicolons
test - Adding/updating tests
chore - Maintenance, dependencies
build - Build system changes
ci - CI/CD configuration
revert - Revert previous commit
Subject Line Rules
- Use imperative mood: "Add feature" not "Added feature"
- Start with lowercase (no capital first letter)
- No period at end
- 50 characters maximum
- Be specific and descriptive
Body Guidelines
- Explain WHAT and WHY, not HOW
- Wrap at 72 characters
- Use bullet points for multiple changes
- Reference issue numbers:
Fixes #123
- Include breaking changes
Examples
Good:
git commit -m "$(cat <<'EOF'
feat: add trading style filtering to history page
Implemented comprehensive search and filtering:
- Multi-criteria filtering (action, type, risk, style)
- Partial symbol search with case-insensitive matching
- LEFT JOIN with trading_styles table
- Extended API with new query parameters
Fixes #456
EOF
)"
Bad:
git commit -m "Fixed stuff"
git commit -m "WIP"
git commit -m "Updated files"
Branching Strategy
Branch Naming
Format: type/description-in-kebab-case
Types:
feature/ - New features
fix/ - Bug fixes
refactor/ - Code improvements
docs/ - Documentation
test/ - Test additions
chore/ - Maintenance
Examples:
feature/trading-style-personas
fix/history-filter-bug
refactor/database-queries
docs/api-documentation
Branch Workflow
git checkout -b feature/new-feature
git add ...
git commit -m "..."
git fetch origin
git rebase origin/main
git push origin feature/new-feature
Branch Management
git branch -a
git checkout branch-name
git branch -d branch-name
git push origin --delete branch-name
git branch -m new-name
Staging Operations
Selective Staging
git add file1.ts file2.ts
git add src/features/
git add .
git add *.ts
git add -p file.ts
Patch Mode Operations
When using git add -p:
y - stage this hunk
n - don't stage this hunk
s - split into smaller hunks
e - manually edit hunk
q - quit
? - help
Unstaging
git reset HEAD
git restore --staged file.ts
git restore --staged src/features/
History Management
Viewing History
git log --oneline -10
git log -5
git log --stat -3
git log -- path/to/file
git log --oneline --graph --all
git log --grep="search term"
git log --author="name"
git log --since="2 weeks ago"
Amending Commits
git add forgotten-file.ts
git commit --amend --no-edit
git commit --amend -m "new message"
⚠️ Warning: Only amend commits that haven't been pushed!
Interactive Rebase
git rebase -i HEAD~3
git rebase -i commit-hash
Options:
pick - keep commit as-is
reword - change commit message
edit - modify commit
squash - combine with previous
fixup - like squash, discard message
drop - remove commit
Squashing Commits
Before pushing:
git rebase -i HEAD~3
Cherry-picking
git cherry-pick commit-hash
git cherry-pick hash1 hash2 hash3
Merging & Rebasing
Merge vs Rebase
Merge:
- Creates merge commit
- Preserves complete history
- Use for: integrating feature branches to main
git checkout main
git merge feature/new-feature
Rebase:
- Rewrites history, linear timeline
- Cleaner history
- Use for: updating feature branch with main changes
git checkout feature/new-feature
git rebase main
Merge Strategies
Fast-forward (default):
git merge feature/branch
No fast-forward (always create merge commit):
git merge --no-ff feature/branch
Squash (combine all commits):
git merge --squash feature/branch
git commit -m "feat: merged feature"
Resolving Conflicts
git status
git diff
git add resolved-file.ts
git rebase --continue
git rebase --abort
Remote Operations
Working with Remotes
git remote -v
git remote add origin https://github.com/user/repo.git
git remote set-url origin new-url
git fetch origin
git pull --rebase origin main
git push origin branch-name
git push --force-with-lease origin branch-name
Pull Request Workflow
git checkout main
git pull origin main
git checkout -b feature/new-feature
git fetch origin
git rebase origin/main
git push origin feature/new-feature
git add .
git commit -m "fix: address review comments"
git push origin feature/new-feature
git checkout main
git pull origin main
git branch -d feature/new-feature
Advanced Techniques
Stashing
git stash
git stash save "work in progress"
git stash list
git stash apply
git stash pop
git stash apply stash@{2}
git stash drop stash@{0}
git stash clear
Tagging
git tag v1.0.0
git tag -a v1.0.0 -m "Release version 1.0.0"
git tag
git push origin v1.0.0
git push origin --tags
git tag -d v1.0.0
git push origin --delete v1.0.0
Bisect (Finding Bugs)
git bisect start
git bisect bad
git bisect good commit-hash
git bisect good
git bisect reset
Reflog (Recovery)
git reflog
git reset --hard commit-hash
git checkout -b recovered-branch commit-hash
Git Ignore
.gitignore Patterns
secret.env
node_modules/
*.log
!important.log
**/debug.log
Common Ignores
node_modules/
vendor/
dist/
build/
*.exe
.env
.env.local
.vscode/
.idea/
*.swp
.DS_Store
Thumbs.db
*.log
logs/
Best Practices Checklist
Before Committing
Branch Management
Commit Quality
Code Review
Anti-Patterns to Avoid
❌ Giant Mixed Commits
git add .
git commit -m "various changes"
Problem: Impossible to review, revert, or understand
Fix: Divide into atomic commits by concern
❌ "Related" Bundling
git add src/components/Form.tsx src/components/PDFExport.tsx src/types/ src/config/
git commit -m "feat: add form and PDF export with new field types"
Problem: "Related to same request" ≠ "Same commit". Cannot revert PDF without losing Form.
Test: Can you revert just ONE of these features independently? No? Split it.
Fix:
git add src/config/ src/types/
git commit -m "feat: add field mapping configuration"
git add src/components/Form.tsx
git commit -m "feat: add editable form component"
git add src/components/PDFExport.tsx
git commit -m "feat: add PDF export with bank-style layout"
❌ Committing Directly to Main
git checkout main
git commit -m "quick fix"
git push
Problem: Bypasses code review, risky
Fix: Always use feature branches
❌ Force Push to Shared Branches
git push --force origin main
Problem: Destroys others' work, breaks history
Fix: Use --force-with-lease and only on your branches
❌ Large Binary Files
git add large-video.mp4
git commit -m "add video"
Problem: Bloats repository size forever
Fix: Use Git LFS or external storage
❌ Committing Secrets
git add .env
git commit -m "add config"
Problem: Security vulnerability, hard to remove
Fix: Use .gitignore, environment variables, secrets management
❌ Meaningless Messages
git commit -m "fix"
git commit -m "update"
git commit -m "wip"
Problem: History is useless for debugging
Fix: Write descriptive, specific commit messages
Quick Reference
Essential Commands
git status
git diff
git diff --cached
git diff --stat
git add file.ts
git add .
git reset HEAD
git restore --staged file.ts
git commit -m "message"
git commit --amend
git branch
git checkout -b branch-name
git branch -d branch-name
git log --oneline -10
git log --stat
git show commit-hash
git fetch origin
git pull --rebase
git push origin branch-name
git stash
git stash pop
Recovery Commands
git reset --soft HEAD~1
git reset --hard HEAD~1
git restore file.ts
git reflog
git checkout -b branch-name commit-hash
Resources
Status: Production-ready ✅
Line Count: ~480 (under 500-line rule) ✅
Coverage: Complete git workflow + atomic commit enforcement ✅