devops

Git Basics for DevOps

Essential Git commands, branching strategies, and workflows used in DevOps


Why Git in DevOps?

Every DevOps pipeline starts with source code. Git is the version control system that tracks every change, enables collaboration, and is the trigger for CI/CD automation.


Essential Git Commands

Setup

Terminal window
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Starting a Repo

Terminal window
git init # new local repo
git clone <url> # copy a remote repo

Daily Workflow

Terminal window
git status # see what's changed
git add . # stage all changes
git add <file> # stage a specific file
git commit -m "message" # save snapshot
git push origin main # push to remote
git pull # fetch + merge from remote

Branching

Terminal window
git branch feature/login # create branch
git checkout feature/login # switch to branch
git checkout -b feature/login # create + switch (shortcut)
git merge feature/login # merge into current branch
git branch -d feature/login # delete branch

Undoing Things

Terminal window
git restore <file> # discard unstaged changes
git reset HEAD <file> # unstage a file
git revert <commit-hash> # undo a commit (safe, creates new commit)
git reset --hard <hash> # dangerous: rewrite history

Viewing History

Terminal window
git log --oneline # compact history
git log --graph --all # visual branch graph
git diff # see unstaged changes
git diff --staged # see staged changes

Branching Strategies

Git Flow

Classic strategy for teams with scheduled releases:

main ──────────────────────────────────────────► (production)
↑ ↑
develop ──────────────────────────────────► (integration)
↑ feature/A ↑ feature/B
  • main β€” always production-ready
  • develop β€” integration branch
  • feature/* β€” one branch per feature
  • release/* β€” stabilization before merging to main
  • hotfix/* β€” emergency patches off main
main ────────────────────────────────────────►
feature/X ──► PR ──► merge to main ──► deploy
  1. Branch off main
  2. Make changes
  3. Open a Pull Request
  4. Review + CI passes
  5. Merge β†’ auto-deploy

Trunk-Based Development (DevOps favorite)

Everyone commits directly to main (or very short-lived branches < 1 day). Requires strong CI and feature flags. Used by Google, Facebook, Netflix.


Git in CI/CD

A typical CI trigger in GitHub Actions:

on:
push:
branches: [main]
pull_request:
branches: [main]

Every push runs tests automatically. This is the foundation of Continuous Integration.


Useful Git Aliases

Terminal window
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.lg "log --oneline --graph --all"

.gitignore

Always exclude secrets, build artifacts, and editor files:

# dependencies
node_modules/
# build output
dist/
build/
*.class
# secrets
.env
*.pem
credentials.json
# editor
.vscode/
.idea/

Quick Reference Card

TaskCommand
See statusgit status
Stage allgit add .
Commitgit commit -m "msg"
Pushgit push origin <branch>
Pullgit pull
New branchgit checkout -b <name>
Mergegit merge <branch>
Historygit log --oneline
Undo filegit restore <file>