25 Git Commands Every DevOps Engineer Must Know
Master the 25 essential Git commands that every DevOps engineer uses daily in 2025. From basic commits to advanced rebasing, cherry-picking, and fixing CI/CD pipeline issues, this comprehensive cheat sheet with real-world examples will boost your productivity and make you unstoppable in any DevOps role.
Introduction
Git is the backbone of modern software delivery. Whether you're automating CI/CD pipelines, managing Infrastructure as Code, or collaborating on Kubernetes manifests, mastering Git is non-negotiable for any DevOps engineer. These 25 commands go beyond basic clone-commit-push and focus on the real-world scenarios you'll face daily: fixing broken builds, cleaning up messy history, debugging production issues, and enforcing team workflows.
Basic but Essential Commands
git clone --depth 1– Faster clones when you don’t need full historygit init --bare– Create bare repositories for CI/CD serversgit config --global alias.co checkout– Create shortcuts (co, ci, st, br)git remote -v– Verify your origin and upstream remotesgit fetch --prune– Remove stale remote-tracking branchesgit pull --rebase– Keep a clean linear history in feature branches, especially useful with Lambda deployments
Branching & Workflow Mastery
git switch -c feature/new-login– Modern way to create and switch branchesgit branch -m old-name new-name– Rename current branch safelygit push -u origin feature-branch– Set upstream tracking on first pushgit branch --merged– Find branches safe to deletegit push origin --delete old-feature– Clean up remote branches
Commit & History Management
| Command | Purpose | DevOps Use Case |
|---|---|---|
git commit --amend |
Fix last commit message or add files | Correct typo before pipeline runs |
git reset --soft HEAD~1 |
Undo commit but keep changes staged | Combine or split commits |
git revert <commit> |
Safely undo changes in shared branches | Hotfix production without rewriting history |
git cherry-pick <commit> |
Apply specific commit to another branch | Backport security fix to release branch |
Stashing & Work-in-Progress
git stash push -m "wip-login-fix"– Save work with descriptive namegit stash list– See all stashed changesgit stash pop stash@{1}– Apply specific stashgit stash drop stash@{0}– Clean up after applyinggit stash --patch– Stash only specific hunks (advanced), perfect when working with RDS monitoring configs
Debugging & Investigation
git log --oneline --graph --all– Visualize complex branch historygit blame filename– Find who changed what and whengit diff --staged– See what’s about to be committedgit bisect start/bad/good– Binary search to find regression commitgit reflog– Recover lost commits after force pushes
Advanced History Rewriting
git rebase -i HEAD~5– Interactive rebase to squash, reorder, editgit push --force-with-lease– Safer alternative to force pushgit filter-branchorgit filter-repo– Remove secrets from historygit update-ref -d refs/original/– Clean up after filter-branch
Cleaning & Maintenance
git gc --aggressive --prune=now – Optimize repository size
git clean -fdx – Remove all untracked files (dangerous – use carefully!)
Collaboration & Automation
git config --global core.hooksPath .githooks– Use project-specific hooksgit config --global init.templateDir ~/.git-template– Enforce team hooksgit cliff– Generate beautiful changelogs automaticallygit secretsorgitleaks– Scan for leaked credentials in CI, especially important when managing secure RDS instances
Conclusion
Mastering these 25 Git commands separates good DevOps engineers from great ones. They enable you to move fast without breaking things, recover from mistakes quickly, enforce clean history in CI/CD pipelines, and automate repetitive tasks. Bookmark this page, practice daily, and soon these commands will become muscle memory that makes you the go-to expert on every team.
Frequently Asked Questions
What’s the difference between git reset and git revert?
reset rewrites history (dangerous on shared branches); revert creates a new commit that undoes changes – safe for main/production.
Is git push --force ever safe?
Use --force-with-lease instead – it protects against overwriting someone else’s work.
How do I recover a deleted branch?
Use git reflog to find the old commit, then git branch recovered-branch <sha>.
What does git rebase do?
It reapplies your commits on top of another branch, creating a linear history instead of merge commits.
Should I squash commits before merging?
Yes, especially in feature branches. Use git rebase -i or squash-and-merge in GitHub/GitLab.
How do I find who introduced a bug?
Use git bisect for binary search or git blame to trace line-by-line authorship.
What is git stash used for?
Temporarily save unfinished work when you need to switch branches urgently.
Can I undo a git rebase gone wrong?
Yes! git reflog shows pre-rebase state, then git reset --hard ORIG_HEAD.
How do I remove sensitive data from Git history?
Use git filter-repo or BFG Repo-Cleaner, then force push and notify team.
What’s the best Git workflow for DevOps teams?
GitOps with feature branches, trunk-based development, or GitHub Flow depending on release frequency.
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Angry
0
Sad
0
Wow
0