Introduction to Git Workflows

Git workflows are standardized processes for using Git in team environments. They define how branches are created, merged, and deployed, ensuring organized collaboration and code quality.

Feature Branch Workflow

The most common workflow where each feature is developed in a dedicated branch.

Basic Feature Branch Process



bash

# 1. Start from main branch
git checkout main
git pull origin main

# 2. Create feature branch
git checkout -b feature/user-authentication
# or: git switch -c feature/user-authentication

# 3. Work on feature (make commits)
git add .
git commit -m "Add login form component"
git commit -m "Implement password validation"
git commit -m "Add user session management"

# 4. Push feature branch
git push -u origin feature/user-authentication

# 5. Create pull request (on GitHub/GitLab)
# 6. After review and approval, merge via PR interface
# 7. Clean up
git checkout main
git pull origin main
git branch -d feature/user-authentication

Branch Naming Conventions



bash

# Feature branches
feature/user-profile
feature/payment-integration
feat/shopping-cart

# Bug fixes
bugfix/header-responsive-issue
hotfix/security-patch
fix/login-error

# Releases
release/v1.2.0
release/2024-q1

# Documentation
docs/api-documentation
docs/deployment-guide

Pull Request Best Practices

Creating Effective Pull Requests



markdown

## Pull Request Template

### Title Format
[Feature/Bugfix/Hotfix]: Brief description

### Description
- **What**: What changes are being made
- **Why**: Why these changes are necessary  
- **How**: How the changes solve the problem

### Changes Made
- [ ] Added user authentication system
- [ ] Updated database schema
- [ ] Added unit tests
- [ ] Updated documentation

### Testing
- [ ] Unit tests pass
- [ ] Integration tests pass
- [ ] Manual testing completed
- [ ] Cross-browser testing done

### Screenshots/Demo
[Include relevant screenshots or demo links]

### Checklist
- [ ] Code follows project style guidelines
- [ ] Self-review completed
- [ ] Added/updated tests
- [ ] Updated documentation
- [ ] No breaking changes (or documented)

PR Review Process



bash

# Reviewer workflow
git fetch origin
git checkout feature/user-authentication
git pull origin feature/user-authentication

# Review code, test locally
npm install
npm test
npm start

# After testing, provide feedback or approve
# Merge strategies:
# 1. Merge commit (preserves branch history)
# 2. Squash and merge (clean linear history)
# 3. Rebase and merge (linear history without merge commit)

GitFlow Workflow

A branching model with specific branch types for different purposes.

GitFlow Branch Structure



main (production-ready code)
├── develop (integration branch)
│   ├── feature/feature-a
│   ├── feature/feature-b
│   └── feature/feature-c
├── release/v1.2.0
└── hotfix/critical-security-fix

GitFlow Commands



bash

# Initialize GitFlow
git flow init

# Start feature
git flow feature start user-profile
# Work on feature...
git flow feature finish user-profile

# Start release
git flow release start 1.2.0
# Prepare release (version bumps, changelog)
git flow release finish 1.2.0

# Emergency hotfix
git flow hotfix start security-patch
# Fix critical issue
git flow hotfix finish security-patch

GitHub Workflow Example



bash

# 1. Fork repository (if external contributor)
# 2. Clone your fork
git clone https://github.com/yourusername/project.git
cd project

# 3. Add upstream remote
git remote add upstream https://github.com/originalowner/project.git

# 4. Create feature branch
git checkout -b feature/new-dashboard

# 5. Make changes and commit
echo "# Dashboard Component" > src/Dashboard.js
git add src/Dashboard.js
git commit -m "Add dashboard component scaffold"

# 6. Keep branch updated with upstream
git fetch upstream
git rebase upstream/main

# 7. Push to your fork
git push origin feature/new-dashboard

# 8. Create Pull Request on GitHub
# Go to GitHub → Compare & pull request

# 9. Address review feedback
git commit -m "Address review comments"
git push origin feature/new-dashboard

# 10. After merge, clean up
git checkout main
git pull upstream main
git branch -d feature/new-dashboard
git push origin --delete feature/new-dashboard

Advanced Git Techniques

Interactive Rebase for Clean History



bash

# Clean up commits before PR
git rebase -i HEAD~3

# In the editor:
# pick abc1234 Add dashboard component
# squash def5678 Fix typo in dashboard
# squash ghi9012 Update dashboard styles
# Result: One clean commit

# Rewrite commit messages
git commit --amend -m "Add responsive dashboard component"

Resolving Merge Conflicts



bash

# When conflicts occur during merge/rebase
git status  # Shows conflicted files

# Edit conflicted files, resolve conflicts
# Remove conflict markers: <<<<<<<, =======, >>>>>>>

# Stage resolved files
git add conflicted-file.js

# Continue rebase/merge
git rebase --continue
# or: git merge --continue

Complete Team Workflow Example



bash

#!/bin/bash
# team-workflow.sh - Example workflow script

# Daily workflow for team member
echo "🔄 Starting daily development workflow..."

# 1. Sync with main
git checkout main
git pull origin main
echo "✅ Main branch updated"

# 2. Create feature branch
FEATURE_NAME="feature/$(date +%Y%m%d)-user-notifications"
git checkout -b $FEATURE_NAME
echo "🌿 Created branch: $FEATURE_NAME"

# 3. Development cycle
echo "💻 Development time! Make your changes..."
# Developer works here...

# 4. Commit changes
git add .
read -p "Enter commit message: " COMMIT_MSG
git commit -m "$COMMIT_MSG"

# 5. Push and create PR
git push -u origin $FEATURE_NAME
echo "🚀 Branch pushed. Create PR on GitHub:"
echo "https://github.com/yourorg/yourproject/compare/$FEATURE_NAME"

# 6. After PR is merged
echo "After PR is merged, run:"
echo "git checkout main"
echo "git pull origin main" 
echo "git branch -d $FEATURE_NAME"

Workflow Configuration Files

.gitignore



gitignore

# Dependencies
node_modules/
*.log
npm-debug.log*

# Build outputs
dist/
build/
*.tsbuildinfo

# Environment variables
.env
.env.local
.env.production

# IDE files
.vscode/
.idea/
*.swp
*.swo

# OS files
.DS_Store
Thumbs.db

.gitmessage Template



# Type: Brief description (50 chars max)

# Longer description if needed (wrap at 72 chars)
# - What was changed?
# - Why was it changed?
# - Any side effects?

# Related issues: #123, #456
# Breaking change: yes/no

GitHub PR Template



markdown

<!-- .github/pull_request_template.md -->
## 📝 Description
Brief description of changes

## 🎯 Type of Change
- [ ] Bug fix
- [ ] New feature  
- [ ] Breaking change
- [ ] Documentation update

## 🧪 Testing
- [ ] Unit tests added/updated
- [ ] Integration tests pass
- [ ] Manual testing completed

## 📷 Screenshots
[Add screenshots for UI changes]

## 📋 Checklist
- [ ] Code follows style guidelines
- [ ] Self-review completed
- [ ] Added tests for new functionality
- [ ] Documentation updated

Workflow Tools Integration

GitHub Actions Workflow Trigger



yaml

# .github/workflows/pr-checks.yml
name: PR Checks
on:
  pull_request:
    branches: [main, develop]
    
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: '18'
      - run: npm install
      - run: npm test
      - run: npm run lint

Pre-commit Hooks



bash

# Install husky for git hooks
npm install --save-dev husky

# Setup pre-commit hook
npx husky add .husky/pre-commit "npm test"
npx husky add .husky/pre-commit "npm run lint"
npx husky add .husky/pre-push "npm run build"

Best Practices Summary

  1. Keep branches focused - One feature per branch
  2. Use descriptive names - Clear branch and commit naming
  3. Small, frequent commits - Easier to review and revert
  4. Rebase vs Merge - Choose based on team preference
  5. Protect main branch - Require PR reviews
  6. Automate checks - Use CI/CD for testing
  7. Clean history - Squash commits when appropriate
  8. Document changes - Good commit messages and PR descriptions