Git Workflow
Branching Strategy
Section titled “Branching Strategy”We use a modified Git Flow workflow that balances flexibility with structure.
Main Branches
Section titled “Main Branches”- Production-ready code
- Protected branch (requires PR approval)
- Automatically deploys to production
- Never commit directly
develop
Section titled “develop”- Integration branch for features
- Staging environment deployment
- Base for feature branches
- Protected branch
Supporting Branches
Section titled “Supporting Branches”Feature Branches
Section titled “Feature Branches”# Create from developgit checkout developgit pull origin developgit checkout -b feature/user-authentication
# Naming conventionfeature/short-descriptionfeature/add-dark-modefeature/user-profile-pageBugfix Branches
Section titled “Bugfix Branches”# Create from developgit checkout developgit checkout -b bugfix/login-timeout
# Naming conventionbugfix/short-descriptionbugfix/fix-memory-leakbugfix/resolve-api-errorHotfix Branches
Section titled “Hotfix Branches”# Create from main (for urgent production fixes)git checkout maingit checkout -b hotfix/security-patch
# Naming conventionhotfix/critical-issue-descriptionDaily Workflow
Section titled “Daily Workflow”Starting a New Feature
Section titled “Starting a New Feature”# 1. Ensure develop is up to dategit checkout developgit pull origin develop
# 2. Create feature branchgit checkout -b feature/new-feature
# 3. Make changes and commit regularlygit add .git commit -m "feat: add user authentication"
# 4. Push to remotegit push -u origin feature/new-feature
# 5. Create pull request when readyKeeping Your Branch Updated
Section titled “Keeping Your Branch Updated”# Regularly sync with developgit checkout developgit pull origin developgit checkout feature/your-featuregit rebase develop
# Or use merge if you prefergit merge developResolving Conflicts
Section titled “Resolving Conflicts”# During rebase/merge, if conflicts occurgit status # See conflicted files
# Edit files to resolve conflicts# Look for markers: <<<<<<<, =======, >>>>>>>
# After resolvinggit add <resolved-files>git rebase --continue # if rebasing# orgit commit # if mergingCommit Guidelines
Section titled “Commit Guidelines”Commit Message Format
Section titled “Commit Message Format”We follow Conventional Commits:
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]Commit Types
Section titled “Commit Types”| Type | Description | Example |
|---|---|---|
feat | New feature | feat: add user registration |
fix | Bug fix | fix: resolve login timeout |
docs | Documentation | docs: update API guide |
style | Code style/formatting | style: fix linting errors |
refactor | Code refactoring | refactor: simplify auth logic |
test | Add/update tests | test: add user service tests |
chore | Maintenance | chore: update dependencies |
perf | Performance | perf: optimize database queries |
ci | CI/CD changes | ci: update deployment pipeline |
Good Commit Examples
Section titled “Good Commit Examples”feat: add password reset functionality
Implements password reset via email with secure token.Includes rate limiting to prevent abuse.
Closes #123
---
fix: resolve race condition in payment processing
The payment handler was processing concurrent requestsincorrectly, leading to duplicate charges.
Fixes #456
---
docs: add authentication flow diagram
---
refactor: extract validation logic to separate module
Improves testability and reusability of validation functions.
---
test: add integration tests for checkout flowCommit Best Practices
Section titled “Commit Best Practices”- ✅ Make atomic commits (one logical change per commit)
- ✅ Write descriptive commit messages
- ✅ Commit early and often
- ✅ Reference issue numbers when applicable
- ✅ Use present tense (“add feature” not “added feature”)
Don’ts
Section titled “Don’ts”- ❌ Don’t commit commented-out code
- ❌ Don’t commit console.log or debug statements
- ❌ Don’t commit sensitive data (API keys, passwords)
- ❌ Don’t commit large binary files (unless necessary)
- ❌ Don’t use vague messages like “fix stuff” or “update”
Pull Request Process
Section titled “Pull Request Process”Creating a Pull Request
Section titled “Creating a Pull Request”-
Push your branch
Terminal window git push origin feature/your-feature -
Open PR on GitHub
- Navigate to repository
- Click “New Pull Request”
- Select your branch
- Fill out PR template
-
PR Title Format
[Type] Brief descriptionExamples:[Feature] Add user authentication[Bugfix] Fix memory leak in image processing[Hotfix] Patch security vulnerability -
PR Description Template
## DescriptionBrief description of changes## Changes Made- Bullet point list of changes- Each significant change on its own line## Testing- How to test these changes- Any specific test cases## Screenshots (if applicable)[Add screenshots]## Related IssuesCloses #123Related to #456## Checklist- [ ] Tests added/updated- [ ] Documentation updated- [ ] No console.log statements- [ ] Code follows style guide- [ ] Security considerations addressed
PR Review Process
Section titled “PR Review Process”For Authors
Section titled “For Authors”-
Self-review first
- Review your own code before requesting review
- Check for debug code, console.logs
- Ensure tests pass
- Run linter
-
Request reviewers
- Assign at least 2 reviewers
- Include domain expert when relevant
- Tag in Slack for urgent reviews
-
Respond to feedback
- Address all comments
- Push additional commits with fixes
- Reply to comments explaining changes
- Re-request review after updates
-
Keep PR updated
- Rebase on develop if conflicts arise
- Keep PR description current
- Update related documentation
For Reviewers
Section titled “For Reviewers”-
Review checklist
- Code follows style guide
- Logic is sound and efficient
- Tests are adequate
- No security vulnerabilities
- Error handling is proper
- Documentation is updated
- No breaking changes (or well documented)
-
Provide constructive feedback
Good: "Consider using async/await here for better readability"Bad: "This code is bad"Good: "This could cause a memory leak. Consider using WeakMap"Bad: "Fix this" -
Use GitHub suggestions
```suggestionconst user = await db.users.findOne({ id: userId }); -
Approval criteria
- All tests pass
- No major issues unresolved
- Code meets quality standards
- Documentation updated
Merging
Section titled “Merging”Merge Requirements
Section titled “Merge Requirements”- ✅ At least 2 approvals
- ✅ All CI checks passing
- ✅ No merge conflicts
- ✅ Branch up to date with base
Merge Strategy
Section titled “Merge Strategy”-
Squash and merge for feature branches (default)
- Creates clean, linear history
- All commits combined into one
-
Merge commit for release branches
- Preserves branch history
-
Rebase and merge for small fixes
- Maintains individual commits
# After PR approval# GitHub will handle the merge via UI# Or from command line:git checkout developgit pull origin developgit merge --squash feature/your-featuregit commitgit push origin developAdvanced Git Operations
Section titled “Advanced Git Operations”Interactive Rebase
Section titled “Interactive Rebase”# Clean up last 3 commitsgit rebase -i HEAD~3
# Commands:# pick = use commit# reword = change commit message# edit = amend commit# squash = combine with previous# drop = remove commitCherry-Pick
Section titled “Cherry-Pick”# Apply specific commit to current branchgit cherry-pick <commit-hash>
# Cherry-pick multiple commitsgit cherry-pick <commit1> <commit2># Save uncommitted changesgit stash
# List stashesgit stash list
# Apply latest stashgit stash pop
# Apply specific stashgit stash apply stash@{2}
# Stash with messagegit stash save "WIP: working on feature"Undoing Changes
Section titled “Undoing Changes”# Undo last commit (keep changes)git reset --soft HEAD~1
# Undo last commit (discard changes)git reset --hard HEAD~1
# Revert a commit (create new commit)git revert <commit-hash>
# Discard local changesgit checkout -- <file>
# Reset to remote stategit fetch origingit reset --hard origin/developGit Hooks
Section titled “Git Hooks”Pre-commit Hook
Section titled “Pre-commit Hook”#!/bin/bash
# Run linternpm run lintif [ $? -ne 0 ]; then echo "Linting failed. Please fix errors before committing." exit 1fi
# Run testsnpm testif [ $? -ne 0 ]; then echo "Tests failed. Please fix before committing." exit 1fiUsing Husky
Section titled “Using Husky”# Install huskynpm install --save-dev husky
# Add to package.json{ "husky": { "hooks": { "pre-commit": "npm run lint && npm test", "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" } }}Best Practices
Section titled “Best Practices”Branch Lifecycle
Section titled “Branch Lifecycle”- Create branch from develop
- Develop and commit changes
- Keep branch updated with develop
- Create pull request
- Address review feedback
- Merge to develop
- Delete branch after merge
# Delete local branchgit branch -d feature/completed-feature
# Delete remote branchgit push origin --delete feature/completed-featureCommit Frequency
Section titled “Commit Frequency”- Commit after completing a logical unit of work
- Don’t commit half-finished features
- Commit before switching contexts
- Create checkpoint commits during long features
Branch Naming
Section titled “Branch Naming”# Goodfeature/user-authenticationbugfix/login-form-validationhotfix/security-patch-cve-2024
# Badfixmy-branchtest123john-devWorking with Remotes
Section titled “Working with Remotes”# List remotesgit remote -v
# Add upstream (for forks)git remote add upstream git@github.com:opticworks/repo.git
# Fetch from upstreamgit fetch upstream
# Sync fork with upstreamgit checkout developgit pull upstream developgit push origin developTroubleshooting
Section titled “Troubleshooting”Accidentally Committed to Wrong Branch
Section titled “Accidentally Committed to Wrong Branch”# Move last commit to new branchgit branch new-featuregit reset --hard HEAD~1git checkout new-featureMerge Conflicts
Section titled “Merge Conflicts”# Abort mergegit merge --abort
# Abort rebasegit rebase --abort
# Use theirs or oursgit checkout --theirs <file> # Use their versiongit checkout --ours <file> # Use our versionRecover Deleted Branch
Section titled “Recover Deleted Branch”# Find commit hashgit reflog
# Recreate branchgit checkout -b recovered-branch <commit-hash>Clean Up Local Branches
Section titled “Clean Up Local Branches”# List merged branchesgit branch --merged
# Delete all merged branchesgit branch --merged | grep -v "main\|develop" | xargs git branch -d
# Prune remote branchesgit remote prune originResources
Section titled “Resources”- Git Documentation
- Conventional Commits
- GitHub Flow
- Internal Git training videos
Getting Help
Section titled “Getting Help”- Slack: #engineering or #git-help
- Pair with senior dev: Schedule time for complex operations
- Documentation: This guide and official Git docs
- Emergency: IT Support can help with repository issues
Remember: When in doubt, ask before force-pushing or rewriting history!