Git is powerful but can become chaotic without conventions. Good workflows reduce conflicts, enable collaboration, and make history meaningful. AI can help you adopt and maintain effective Git practices.
Branching Strategies#
GitHub Flow (Recommended for Most Teams)#
main (always deployable)
│
├── feature/user-auth
│ └── PR → main
│
├── feature/payment-integration
│ └── PR → main
│
└── fix/login-bug
└── PR → main
Rules:
mainis always deployable- Branch from
mainfor any change - Open PR for review
- Merge to
mainand deploy
GitFlow (For Scheduled Releases)#
main ─────────────────────────────────────────
│ ▲
│ │ (release merge)
develop ──────────────────────────────────────
│ │ ▲
│ │ │ (feature merge)
feature/a feature/b ───┘
When to use: Mobile apps, versioned products, regulated industries.
Trunk-Based Development#
main ──●──●──●──●──●──●──●──●──●──
│ │ │ │ │ │ │ │ │
└──┴──┴──┴──┴──┴──┴──┴──┘
(small, frequent commits)
Rules:
- Commit directly to
main(or very short-lived branches) - Use feature flags for incomplete work
- Deploy continuously
Commit Best Practices#
Commit Message Format#
type(scope): subject
body (optional)
footer (optional)
Types:
feat: New featurefix: Bug fixdocs: Documentationstyle: Formattingrefactor: Code restructuringtest: Adding testschore: Maintenance
Examples:
feat(auth): add OAuth2 Google login
Implements Google OAuth2 flow with token refresh.
Stores tokens securely in encrypted session.
Closes #123
fix(cart): prevent negative quantities
Users could enter negative numbers in quantity input,
resulting in negative totals. Added validation.
Atomic Commits#
Each commit should be a single logical change:
1# ❌ Bad: Multiple unrelated changes
2git commit -m "Fix login bug, add new header, update dependencies"
3
4# ✅ Good: Separate commits
5git commit -m "fix(auth): handle expired session redirect"
6git commit -m "feat(ui): add responsive header component"
7git commit -m "chore(deps): update React to 18.2"Interactive Rebase for Clean History#
1# Before pushing, clean up commits
2git rebase -i HEAD~5
3
4# In editor:
5pick abc123 feat: add user model
6squash def456 fix typo
7squash ghi789 add missing field
8pick jkl012 feat: add user API
9pick mno345 test: add user testsPull Request Practices#
PR Template#
1## Description
2Brief description of changes.
3
4## Type of Change
5- [ ] Bug fix
6- [ ] New feature
7- [ ] Breaking change
8- [ ] Documentation
9
10## Testing
11- [ ] Unit tests added/updated
12- [ ] Integration tests added/updated
13- [ ] Manual testing completed
14
15## Checklist
16- [ ] Code follows style guidelines
17- [ ] Self-reviewed
18- [ ] Documentation updated
19- [ ] No console.logs or debug codePR Size Guidelines#
Small PR (Ideal):
- < 200 lines changed
- Single concern
- Reviewable in 15 minutes
Medium PR (Acceptable):
- 200-500 lines
- Related changes
- Reviewable in 30 minutes
Large PR (Avoid):
- > 500 lines
- Consider splitting
- Hard to review thoroughly
Review Etiquette#
As Author:
- Respond to all comments
- Don't take feedback personally
- Explain decisions when asked
As Reviewer:
- Be constructive, not critical
- Suggest, don't demand
- Approve when "good enough"
Handling Common Scenarios#
Merge vs Rebase#
1# Merge: Preserves history, creates merge commit
2git checkout feature
3git merge main
4
5# Rebase: Linear history, rewrites commits
6git checkout feature
7git rebase mainUse merge when: Preserving context matters, shared branches Use rebase when: Clean history desired, personal branches
Resolving Conflicts#
1# During merge/rebase
2git status # See conflicted files
3
4# Edit files to resolve conflicts
5# Look for conflict markers:
6<<<<<<< HEAD
7your changes
8=======
9their changes
10>>>>>>> branch
11
12# After resolving
13git add resolved-file.js
14git rebase --continue # or git merge --continueRecovering from Mistakes#
1# Undo last commit (keep changes)
2git reset --soft HEAD~1
3
4# Undo last commit (discard changes)
5git reset --hard HEAD~1
6
7# Recover deleted branch
8git reflog
9git checkout -b recovered-branch abc1234
10
11# Undo a pushed commit (creates new commit)
12git revert abc1234Stashing Work#
1# Save work in progress
2git stash
3
4# Save with message
5git stash push -m "WIP: user feature"
6
7# List stashes
8git stash list
9
10# Apply and remove
11git stash pop
12
13# Apply specific stash
14git stash apply stash@{2}Advanced Patterns#
Git Hooks#
# .git/hooks/pre-commit
#!/bin/sh
npm run lint
npm run testWith Husky:
1// package.json
2{
3 "husky": {
4 "hooks": {
5 "pre-commit": "lint-staged",
6 "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
7 }
8 },
9 "lint-staged": {
10 "*.{js,ts}": ["eslint --fix", "prettier --write"]
11 }
12}Bisect for Bug Finding#
1# Start bisect
2git bisect start
3git bisect bad # Current commit is bad
4git bisect good abc123 # Known good commit
5
6# Git checks out middle commit
7# Test and mark:
8git bisect good # or git bisect bad
9
10# Repeat until found
11# Reset when done
12git bisect resetWorktrees for Parallel Work#
1# Work on multiple branches simultaneously
2git worktree add ../hotfix hotfix-branch
3git worktree add ../feature feature-branch
4
5# Each worktree is independent
6cd ../hotfix
7# Make changes, commit, push
8
9# Remove when done
10git worktree remove ../hotfixTeam Conventions#
Branch Naming#
feature/TICKET-123-user-authentication
fix/TICKET-456-login-redirect
hotfix/critical-security-patch
docs/update-api-documentation
refactor/extract-payment-service
Protected Branches#
1# GitHub branch protection
2main:
3 - Require PR reviews (1-2)
4 - Require status checks
5 - Require up-to-date branches
6 - No force push
7 - No deletionsCode Owners#
# .github/CODEOWNERS
*.js @frontend-team
*.ts @frontend-team
/api/** @backend-team
/infrastructure/** @platform-team
*.md @docs-team
Conclusion#
Good Git practices make collaboration smoother and history meaningful. Choose a workflow that fits your team, enforce it with automation, and document conventions clearly.
AI helps write better commit messages, resolve conflicts, and understand complex Git operations. The investment in Git hygiene pays dividends in easier debugging, cleaner reviews, and smoother onboarding.