Version Control with Git
Git is the foundation of modern DevOps workflows. Every change to code and infrastructure is tracked, enabling collaboration, rollback, and auditability.
Git Workflow for DevOps
Feature Branch Workflow:
main โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ (production)
\ / \ /
feature-A โโโโโโโโโโโโโ โโโโโ
\ /
feature-B โโโโโโโโโ
Steps:
1. Create feature branch from main
2. Make changes, commit frequently
3. Push branch and open Pull Request
4. Code review + automated CI tests
5. Merge to main after approval
6. CD pipeline deploys to production
Essential Git Commands
# Create and switch to a new branch
git checkout -b feature/new-api
# Stage and commit changes
git add .
git commit -m "Add new API endpoint"
# Push to remote
git push origin feature/new-api
# Merge with conflict resolution
git checkout main
git merge feature/new-api
# View history and blame
git log --oneline --graph
git blame config/nginx.conf
Git Hooks for DevOps
Git hooks enable automated checks before code enters the pipeline:
# .git/hooks/pre-commit
#!/bin/sh
echo "Running linter..."
npm run lint
if [ $? -ne 0 ]; then
echo "Lint failed. Fix errors before committing."
exit 1
fi
# Common hooks:
# pre-commit โ Run linting, formatting checks
# commit-msg โ Validate commit message format
# pre-push โ Run tests before pushing
Git Best Practices for DevOps
- Small, focused commits โ Easy to review and rollback
- Conventional commits โ
feat:,fix:,chore:prefixes - Protected branches โ Require PR reviews and passing CI
- Monorepo vs polyrepo โ Choose based on team size and coupling
- GitOps โ Use Git as the single source of truth for infrastructure