Git & Version Control

Master Git workflows, branching strategies, collaboration patterns, and GitOps for modern DevOps teams.

Git Basics

Beginner
# Configure Git
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main

# Initialize a repository
git init
git add .
git commit -m "Initial commit"

# Basic workflow
git status                   # Check what's changed
git add file.txt             # Stage specific file
git add .                    # Stage all changes
git commit -m "feat: add user auth"  # Commit with message
git log --oneline --graph    # View commit history

Branching

Beginner
# Create and switch to a branch
git checkout -b feature/user-auth

# List branches
git branch -a

# Switch branches
git checkout main

# Merge a branch
git checkout main
git merge feature/user-auth

# Delete a branch
git branch -d feature/user-auth
git push origin --delete feature/user-auth

Remote Repositories

Beginner
# Clone a repository
git clone https://github.com/user/repo.git

# Add remote
git remote add origin https://github.com/user/repo.git

# Push changes
git push origin main
git push -u origin feature/new-api

# Pull changes
git pull origin main

# Fetch without merging
git fetch origin

Git Workflows

Intermediate

Trunk-Based Development (Recommended for DevOps)

Git Flow

Merge vs Rebase

Intermediate
# Merge — preserves history, creates merge commit
git checkout main
git merge feature/api

# Rebase — linear history, rewrites commits
git checkout feature/api
git rebase main

# Interactive rebase (squash, reword, reorder)
git rebase -i HEAD~5

# Golden rule: Never rebase public/shared branches

Advanced Commands

Intermediate
# Stash changes
git stash
git stash list
git stash pop
git stash apply stash@{0}

# Cherry-pick a commit
git cherry-pick abc1234

# Bisect — find the commit that introduced a bug
git bisect start
git bisect bad               # Current commit is bad
git bisect good v1.0         # v1.0 was good
# Git will checkout commits for you to test
git bisect good              # or git bisect bad
git bisect reset             # When done

# Reset (dangerous — know what you're doing)
git reset --soft HEAD~1      # Undo commit, keep staged
git reset --mixed HEAD~1     # Undo commit, unstage
git reset --hard HEAD~1      # Undo commit, discard changes

# Reflog — recover lost commits
git reflog
git checkout HEAD@{5}

GitOps

Advanced

GitOps uses Git as the single source of truth for declarative infrastructure and applications. Any changes to the system are made through Git commits.

GitOps Principles

1. Declarative configuration in Git. 2. Automated sync (ArgoCD, Flux). 3. Git as the source of truth. 4. Self-healing — drift is automatically corrected.

Git Hooks

Advanced
# .git/hooks/pre-commit
#!/bin/bash
# Run linting before commit
npm run lint
if [ $? -ne 0 ]; then
  echo "❌ Linting failed. Fix errors before committing."
  exit 1
fi

# .git/hooks/commit-msg
#!/bin/bash
# Enforce conventional commits
if ! grep -qE "^(feat|fix|docs|style|refactor|test|chore|ci|perf)(\(.+\))?: .+" "$1"; then
  echo "❌ Commit message must follow conventional commits format"
  echo "Example: feat(auth): add OAuth2 support"
  exit 1
fi

# Use Husky for managing hooks (npm projects)
npx husky init
echo "npm run lint" > .husky/pre-commit