Git Commands Every DevOps Engineer Must Know (2026 Complete Guide)

Git Commands Every DevOps Engineer Must Know (2026 Complete Guide)

Last Updated: March 2026 | 17 min read | For DevOps Beginners & Intermediate Engineers

If Linux is the foundation of DevOps, Git is the backbone. Every pipeline starts with a git push. Every deployment is triggered by a commit. Every rollback depends on your ability to navigate Git history confidently.

Yet most beginners only know git add, git commit, and git push — and panic the moment something goes wrong in a real repository.

This guide covers every Git command you'll actually use as a DevOps engineer — from the basics to the advanced techniques that senior engineers use daily. With real examples you can try right now.


Table of Contents

  1. Why Git Matters in DevOps
  2. Git Basics — The Commands You Use Every Day
  3. Branching & Merging
  4. Remote Repository Commands
  5. Undoing Mistakes
  6. Git Log & History
  7. Advanced Git Commands
  8. Git in CI/CD Pipelines
  9. Git Best Practices for DevOps
  10. Common Git Interview Questions
  11. Quick Reference Cheat Sheet

1. Why Git Matters in DevOps

Git is not just a tool for developers. As a DevOps engineer you use Git constantly:

  • CI/CD pipelines trigger on git push — every deployment starts here
  • Infrastructure as Code — your Terraform, Ansible, and Helm files live in Git
  • GitOps — tools like ArgoCD watch Git repos and sync Kubernetes clusters automatically
  • Rollbacks — when a deployment fails, you revert to a previous Git commit
  • Collaboration — pull requests, code reviews, and branch strategies are all Git workflows
  • Audit trail — Git history shows exactly who changed what and when

If you can't navigate Git confidently, you'll be blocked constantly in real DevOps work.


2. Git Basics — The Commands You Use Every Day

git init — Initialise a Repository

# Create a new Git repository in the current folder
$ git init

# Create a new repo with a specific name
$ git init my-project

git clone — Copy a Repository

# Clone a remote repository
$ git clone https://github.com/username/repo.git

# Clone into a specific folder name
$ git clone https://github.com/username/repo.git my-folder

# Clone a specific branch only
$ git clone -b main https://github.com/username/repo.git

git status — See What's Changed

$ git status

# Output:
On branch main
Changes not staged for commit:
  modified:   app.py
Untracked files:
  new-file.txt

git add — Stage Changes

# Stage a specific file
$ git add app.py

# Stage all changed files
$ git add .

# Stage parts of a file interactively
$ git add -p app.py

Pro tip: Use git add -p when you've made multiple unrelated changes to a file and want to commit them separately. This is a habit that separates junior engineers from senior ones.

git commit — Save Changes

# Commit with a message
$ git commit -m "Add health check endpoint to Docker container"

# Stage all tracked files and commit in one step
$ git commit -am "Fix nginx config for production"

# Amend the last commit message (before pushing)
$ git commit --amend -m "Fix nginx config for production environment"

Good commit message format:

type(scope): short description

Examples:
feat(pipeline): add SonarQube quality gate
fix(docker): resolve CrashLoopBackOff in healthcheck
chore(terraform): update AWS provider to v5.0
docs(readme): add deployment instructions

git push — Upload Changes

# Push to remote
$ git push origin main

# Push a new branch for the first time
$ git push -u origin feature/add-monitoring

# Force push (use carefully — only on your own branches!)
$ git push --force origin feature/my-branch

git pull — Download Latest Changes

# Pull latest changes from remote
$ git pull origin main

# Pull with rebase instead of merge (cleaner history)
$ git pull --rebase origin main

3. Branching & Merging

Branches are how teams work on features, fixes, and releases in parallel without stepping on each other.

git branch — Manage Branches

# List all local branches
$ git branch

# List all branches including remote
$ git branch -a

# Create a new branch
$ git branch feature/add-prometheus

# Delete a branch (after merging)
$ git branch -d feature/add-prometheus

# Force delete a branch (even if not merged)
$ git branch -D feature/old-experiment

# Rename current branch
$ git branch -m new-branch-name

git checkout / git switch — Change Branches

# Switch to an existing branch
$ git checkout main
$ git switch main          # newer syntax

# Create and switch to a new branch in one command
$ git checkout -b feature/add-grafana
$ git switch -c feature/add-grafana    # newer syntax

# Switch to previous branch (very useful!)
$ git checkout -

git merge — Combine Branches

# Merge a feature branch into main
$ git checkout main
$ git merge feature/add-grafana

# Merge with a commit message (no fast-forward)
$ git merge --no-ff feature/add-grafana -m "Merge: add Grafana dashboard"

# Abort a merge that has gone wrong
$ git merge --abort

git rebase — Rewrite History

# Rebase your branch on top of main
$ git checkout feature/my-feature
$ git rebase main

# Interactive rebase — squash, edit, reorder commits
$ git rebase -i HEAD~3    # Interact with last 3 commits

# Abort a rebase
$ git rebase --abort

# Continue after resolving conflicts
$ git rebase --continue

Resolving Merge Conflicts

When Git can't automatically merge, you get a conflict. Open the conflicted file — you'll see:

<<<<<<< HEAD
server {
    listen 80;
=======
server {
    listen 8080;
>>>>>>> feature/add-nginx

Edit the file to keep what you want, remove the conflict markers, then:

$ git add nginx.conf
$ git commit -m "Resolve merge conflict in nginx.conf"

4. Remote Repository Commands

git remote — Manage Remote Connections

# View remote connections
$ git remote -v

# Add a new remote
$ git remote add origin https://github.com/username/repo.git

# Change remote URL
$ git remote set-url origin https://github.com/username/new-repo.git

# Remove a remote
$ git remote remove origin

git fetch — Download Without Merging

# Fetch all changes from remote without merging
$ git fetch origin

# Fetch a specific branch
$ git fetch origin main

# See what changed after fetching
$ git diff origin/main

Fetch vs Pull:

  • git fetch — downloads changes but doesn't apply them. Safe to run anytime.
  • git pull — downloads AND merges changes. Can cause conflicts.

Tagging Releases

# Create a version tag
$ git tag v1.0.0

# Push tags to remote
$ git push origin --tags

# Delete a remote tag
$ git push origin --delete v1.0.0

5. Undoing Mistakes

git restore — Discard Uncommitted Changes

# Discard changes to a specific file
$ git restore app.py

# Discard ALL uncommitted changes
$ git restore .

# Unstage a file
$ git restore --staged app.py

git reset — Undo Commits

# Undo last commit but keep the changes (soft reset)
$ git reset --soft HEAD~1

# Undo last commit and unstage changes (mixed reset)
$ git reset HEAD~1

# Undo last commit and DELETE the changes (hard reset — careful!)
$ git reset --hard HEAD~1

git revert — Safely Undo a Commit

# Create a new commit that undoes a previous commit
$ git revert abc1234

# Revert without opening the commit message editor
$ git revert abc1234 --no-edit

Reset vs Revert: Use git revert on shared/production branches — it's safe. Use git reset only on your own local branches.

git stash — Temporarily Save Work

# Save current changes without committing
$ git stash

# Stash with a descriptive name
$ git stash save "WIP: add prometheus scrape config"

# List all stashes
$ git stash list

# Apply the most recent stash
$ git stash pop

# Apply a specific stash
$ git stash apply stash@{2}

# Clear all stashes
$ git stash clear

Real world use case: You're halfway through a change when your manager asks you to urgently fix a bug on main. Stash your work, fix the bug, push, then git stash pop to continue where you left off.


6. Git Log & History

git log — View Commit History

# Full commit history
$ git log

# Compact one-line format
$ git log --oneline

# Visual branch graph
$ git log --oneline --graph --all

# Last 5 commits
$ git log -5

# Commits by a specific author
$ git log --author="Vishwa"

# Search commit messages
$ git log --grep="fix"

git diff — See What Changed

# Changes not yet staged
$ git diff

# Changes that are staged
$ git diff --staged

# Difference between two branches
$ git diff main feature/add-monitoring

# Just show which files changed
$ git diff --name-only main feature/add-monitoring

git blame — See Who Changed What

# See who last modified each line of a file
$ git blame nginx.conf

7. Advanced Git Commands

git cherry-pick — Apply Specific Commits

# Apply a specific commit from another branch
$ git cherry-pick abc1234

# Cherry-pick without committing
$ git cherry-pick abc1234 --no-commit

Real world use case: A critical bug fix was committed to a feature branch but needs to go to production immediately. Cherry-pick just that commit onto main without merging the whole feature branch.

git bisect — Find Which Commit Broke Something

# Start bisect
$ git bisect start

# Mark current commit as bad
$ git bisect bad

# Mark a known good commit
$ git bisect good v1.0.0

# After testing each checkout Git gives you:
$ git bisect good    # or
$ git bisect bad

# When done
$ git bisect reset

git reflog — Recover Lost Commits

# See ALL Git operations including resets
$ git reflog

# Recover a commit after accidental reset
$ git reset --hard HEAD@{3}

Pro tip: git reflog is your safety net. Even after git reset --hard, your commits are still recoverable for 90 days. This has saved countless engineers from disaster.


8. Git in CI/CD Pipelines

Pipeline Triggers Based on Git Events

# GitHub Actions example
on:
  push:
    branches: [main]          # Deploy to production
  push:
    branches: [develop]       # Deploy to staging
  pull_request:
    branches: [main]          # Run tests only
  push:
    tags: ['v*']              # Build and publish release

Tagging Releases for Deployments

# Tag a release
$ git tag -a v2.1.0 -m "Release v2.1.0: Add Prometheus monitoring"
$ git push origin v2.1.0

# CI/CD pipeline detects the tag and automatically:
# 1. Builds Docker image tagged v2.1.0
# 2. Pushes to ECR/ACR
# 3. Deploys to production via Helm

Rolling Back a Deployment Using Git

# Find the last good commit
$ git log --oneline

# Revert the bad commit
$ git revert abc1234
$ git push origin main

# CI/CD pipeline detects the push and redeploys

9. Git Best Practices for DevOps

Write meaningful commit messages:

# Bad
$ git commit -m "fix"
$ git commit -m "changes"

# Good
$ git commit -m "fix: resolve CrashLoopBackOff in payment-service container"
$ git commit -m "feat: add Prometheus scrape config for API gateway"

Never commit sensitive data. Never commit passwords, API keys, tokens, or .env files.

Always use a .gitignore file:

.env
*.tfstate
*.tfstate.backup
.terraform/
*.pem
*.key
kubeconfig

Keep commits small and focused. One commit = one logical change.

Protect your main branch — enable branch protection rules in GitHub/GitLab requiring PR reviews and CI to pass before merging.


10. Common Git Interview Questions

Q: What is the difference between git fetch and git pull?
git fetch downloads changes from remote but doesn't apply them. git pull downloads and automatically merges them. Use fetch when you want to review changes before merging.

Q: What is the difference between git merge and git rebase?
Merge preserves the full commit history and creates a merge commit. Rebase rewrites history to create a linear sequence of commits. Rebase is cleaner but should only be used on your own local branches.

Q: How do you undo a commit that has already been pushed?
Use git revert — it creates a new commit that undoes the changes without rewriting history. Never use git reset on shared branches.

Q: What is git stash and when would you use it?
Git stash temporarily saves your uncommitted changes so you can switch branches. You'd use it when you need to urgently fix something on another branch but aren't ready to commit your current work.

Q: What is a merge conflict and how do you resolve it?
A merge conflict happens when two branches modify the same part of a file differently. You resolve it by manually editing the conflicted file, removing the conflict markers, then staging and committing the resolved file.

Q: What is git cherry-pick?
Cherry-pick applies a specific commit from one branch onto another without merging the entire branch. Useful for applying urgent bug fixes to production without deploying unfinished features.

Q: What is GitOps?
GitOps is a DevOps practice where Git is the single source of truth for infrastructure and application configuration. Tools like ArgoCD watch a Git repo and automatically sync the Kubernetes cluster to match the repo state.


11. Quick Reference Cheat Sheet

CommandWhat it does
git clone <url>Copy a remote repository
git statusSee what's changed
git add .Stage all changes
git commit -m "msg"Save staged changes
git push origin mainUpload to remote
git pull --rebaseDownload and rebase
git checkout -b branchCreate and switch branch
git stashTemporarily save work
git stash popRestore stashed work
git log --oneline --graphVisual commit history
git revert <commit>Safely undo a commit
git cherry-pick <commit>Apply specific commit
git bisectFind the buggy commit
git reflogRecover lost commits
git tag v1.0.0Tag a release
git blame <file>See who changed what

Wrapping Up

Git is a skill that compounds dramatically over time. The engineers who are fastest and most confident in production incidents are usually the ones who know Git deeply — they can trace bugs to exact commits, roll back deployments in seconds, and navigate complex branch histories without hesitation.

Your Git learning path:

  • Week 1: Master the basics — add, commit, push, pull, branch, merge
  • Week 2: Get comfortable with stash, revert, reset, and log
  • Week 3: Practice cherry-pick, rebase, and bisect on a test repo
  • Week 4: Set up a real CI/CD pipeline triggered by Git events

Found this helpful? Share it with a fellow DevOps engineer. Have a question or suggestion? Reach out at vishwasekar43@gmail.com

— Vishwa, DevOpsVault

Comments

Popular posts from this blog

Linux Commands Every DevOps Engineer Must Know (2026 Complete Guide)

Docker Day 1: Containers Explained — VM vs Docker, Lifecycle & Buildah

Kubernetes Day 7: ConfigMaps & Secrets — Managing Config the Right Way