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
git config --global user.name "Your Name"git config --global user.email "you@example.com"Starting a Repo
git init # new local repogit clone <url> # copy a remote repoDaily Workflow
git status # see what's changedgit add . # stage all changesgit add <file> # stage a specific filegit commit -m "message" # save snapshotgit push origin main # push to remotegit pull # fetch + merge from remoteBranching
git branch feature/login # create branchgit checkout feature/login # switch to branchgit checkout -b feature/login # create + switch (shortcut)git merge feature/login # merge into current branchgit branch -d feature/login # delete branchUndoing Things
git restore <file> # discard unstaged changesgit reset HEAD <file> # unstage a filegit revert <commit-hash> # undo a commit (safe, creates new commit)git reset --hard <hash> # dangerous: rewrite historyViewing History
git log --oneline # compact historygit log --graph --all # visual branch graphgit diff # see unstaged changesgit diff --staged # see staged changesBranching Strategies
Git Flow
Classic strategy for teams with scheduled releases:
main βββββββββββββββββββββββββββββββββββββββββββΊ (production) β βdevelop βββββββββββββββββββββββββββββββββββΊ (integration) β feature/A β feature/Bmainβ always production-readydevelopβ integration branchfeature/*β one branch per featurerelease/*β stabilization before merging to mainhotfix/*β emergency patches off main
GitHub Flow (simpler, recommended)
main βββββββββββββββββββββββββββββββββββββββββΊ feature/X βββΊ PR βββΊ merge to main βββΊ deploy- Branch off
main - Make changes
- Open a Pull Request
- Review + CI passes
- 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
git config --global alias.st statusgit config --global alias.co checkoutgit config --global alias.br branchgit config --global alias.lg "log --oneline --graph --all".gitignore
Always exclude secrets, build artifacts, and editor files:
# dependenciesnode_modules/
# build outputdist/build/*.class
# secrets.env*.pemcredentials.json
# editor.vscode/.idea/Quick Reference Card
| Task | Command |
|---|---|
| See status | git status |
| Stage all | git add . |
| Commit | git commit -m "msg" |
| Push | git push origin <branch> |
| Pull | git pull |
| New branch | git checkout -b <name> |
| Merge | git merge <branch> |
| History | git log --oneline |
| Undo file | git restore <file> |