Basic Git Commands Every DevOps Engineer Must Know
📌 DevOps Perspective
Git commands are your daily tools for infrastructure code, configuration management, and CI/CD pipelines. Mastering these basics is non-negotiable for professional DevOps work.
Git Quick Reference
git init | git clone
git add | git commit | git status
git push | git pull | git fetch
git branch | git checkout | git merge
Repository Commands
Basic Workflow Commands
Viewing History
Remote Operations
Branching Commands
📋 Typical DevOps Git Workflow
git clone https://github.com/company/infra.git
git checkout -b add-monitoring
git status | git diff
git add . | git commit -m "Add Prometheus monitoring"
git push origin add-monitoring
(CI/CD pipeline runs automatically)
git checkout main | git pull | git merge add-monitoring
💡 DevOps Command Patterns
Quick Status Check: git status -s
Add & Commit in One: git commit -am "message"
Undo Last Commit (Keep Changes): git reset --soft HEAD~1
View Last Commit: git show --name-only
Clean Local Branches: git branch --merged | grep -v "\\*\\|main" | xargs -n 1 git branch -d
Frequently Asked Questions (FAQ)
git add . adds all new and modified files in the current directory and subdirectories, but NOT deleted files.
git add -A adds ALL changes: new files, modified files, AND deleted files from the entire working tree.
For DevOps work with infrastructure code, usually git add . is sufficient unless you've deleted configuration files.
git pull = git fetch + git merge
Use git fetch when you want to see what changed on remote without immediately merging. Good for reviewing team changes before updating your work.
Use git pull when you want to immediately update your branch with remote changes. Add --rebase for cleaner history: git pull --rebase origin main
DevOps tip: Use fetch first to review infrastructure changes, then merge carefully.
Follow this format: type(scope): description
Examples for DevOps:
• feat(terraform): add vpc module for staging
• fix(ansible): resolve nginx configuration error
• docs(k8s): update deployment guide
• chore(ci): update jenkins pipeline
• test(docker): add healthcheck tests
Keep it under 50 characters for the subject line.
Detached HEAD means you're not on a branch, but looking at a specific commit. Common when you check out a commit hash or tag.
To fix:
1. Create new branch from current state: git checkout -b new-branch-name
2. Or go back to previous branch: git checkout - (dash means previous branch)
In DevOps, you might enter detached HEAD when checking out specific infrastructure versions for debugging.
Keep changes, remove commit:
git reset --soft HEAD~1
Discard changes, remove commit:
git reset --hard HEAD~1 (WARNING: loses changes!)
Just change commit message:
git commit --amend -m "new message"
DevOps tip: Use --soft when you need to fix infrastructure code before committing.
Simple Git Flow for DevOps:
1. main/master - Production infrastructure
2. staging - Staging environment
3. develop - Development environment
4. feature/* - Specific infrastructure changes
Even Simpler (GitHub Flow):
• main always deployable
• Create feature branches from main
• Merge via PR with CI/CD checks
• Auto-deploy to staging on merge
Choose based on team size and deployment frequency.
Use git blame:
git blame terraform/main.tf
Or with line numbers:
git blame -L 10,20 docker-compose.yml
This shows who last modified each line, which is crucial for debugging infrastructure changes and understanding why certain configurations exist.