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

Start Project: git init | git clone
Daily Work: git add | git commit | git status
Remote Work: git push | git pull | git fetch
Branching: git branch | git checkout | git merge

Repository Commands

git init
Initialize a new Git repository
git init [directory-name]
# Initialize current directory as Git repo git init # Initialize specific directory git init my-project # DevOps Use: Starting new infrastructure code project
git clone
Copy an existing repository
git clone [repository-url] [directory]
# Clone repository git clone https://github.com/company/terraform-repo.git # Clone to specific directory git clone https://github.com/company/ansible-playbooks.git ./ansible # DevOps Use: Getting infrastructure code from remote repos

Basic Workflow Commands

git status
Check status of working directory
git status [options]
# Basic status check git status # Short status (more compact) git status -s # DevOps Use: Check what Terraform/Ansible files changed before commit
git add
Stage changes for commit
git add [file(s)] | git add . | git add -A
# Stage specific file git add terraform/main.tf # Stage all changes in current directory git add . # Stage all changes (including deletions) git add -A # Stage interactively git add -p # DevOps Use: Carefully stage infrastructure changes
git commit
Save staged changes to repository
git commit [-m "message"] [options]
# Commit with message git commit -m "Add kubernetes deployment manifests" # Open editor for detailed message git commit # Amend last commit (fix commit message) git commit --amend -m "Fixed: Update docker-compose.yml" # DevOps Use: Use descriptive messages for infrastructure changes

Viewing History

git log
Show commit history
git log [options]
# Basic log git log # One-line summary git log --oneline # Show last 5 commits git log -5 # Show graph with branches git log --graph --oneline --decorate --all # DevOps Use: Track infrastructure changes over time
git show
Show details of a commit
git show [commit-hash]
# Show latest commit git show # Show specific commit git show abc123 # DevOps Use: Review specific infrastructure change
git diff
Show changes between commits, branches, etc.
git diff [options]
# Show unstaged changes git diff # Show staged changes git diff --staged # Compare two branches git diff main..feature-branch # DevOps Use: Review Terraform/configuration changes

Remote Operations

git remote
Manage remote repositories
git remote [command]
# List remotes git remote -v # Add remote git remote add origin https://github.com/user/repo.git # Rename remote git remote rename origin upstream # DevOps Use: Connect to GitHub/GitLab/GitLab repos
git push
Upload local commits to remote
git push [remote] [branch]
# Push to origin/main git push origin main # Push specific branch git push origin feature-branch # Force push (use with caution!) git push -f origin main # DevOps Use: Send infrastructure code to CI/CD pipeline
git pull
Download and merge from remote
git pull [remote] [branch]
# Pull latest changes git pull origin main # Pull with rebase git pull --rebase origin main # DevOps Use: Get latest infrastructure updates from team
git fetch
Download from remote without merging
git fetch [remote]
# Fetch all from origin git fetch origin # Fetch specific branch git fetch origin main # Fetch all remotes git fetch --all # DevOps Use: Check for remote changes before merging

Branching Commands

git branch
List, create, or delete branches
git branch [options] [branch-name]
# List branches git branch # Create new branch git branch feature-xyz # Delete branch git branch -d old-branch # Force delete git branch -D broken-branch # DevOps Use: Separate environments (dev, staging, prod)
git checkout
Switch branches or restore files
git checkout [branch-name] | git checkout -b [new-branch]
# Switch to existing branch git checkout main # Create and switch to new branch git checkout -b feature-abc # Restore file from last commit git checkout -- terraform/main.tf # DevOps Use: Switch between environment configurations
git merge
Combine branch histories
git merge [branch-name]
# Merge feature into main git checkout main git merge feature-branch # Merge with commit message git merge feature-branch -m "Merge feature for deployment" # DevOps Use: Deploy infrastructure changes to production

📋 Typical DevOps Git Workflow

1
Clone Infrastructure Repository
git clone https://github.com/company/infra.git
2
Create Feature Branch
git checkout -b add-monitoring
3
Make Changes & Check Status
git status | git diff
4
Stage and Commit
git add . | git commit -m "Add Prometheus monitoring"
5
Push to Remote
git push origin add-monitoring
6
Create PR/Merge Request
(CI/CD pipeline runs automatically)
7
Merge to Main
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)

What's the difference between git add . and git add -A?

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.

When should I use git pull vs git fetch?

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.

How do I write good commit messages for DevOps?

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.

What does "detached HEAD state" mean and how do I fix it?

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.

How do I undo the last commit but keep my changes?

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.

What's the best branching strategy for DevOps projects?

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.

How do I see who changed a specific line in my infrastructure code?

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.

Previous: Installing Git Next: Git Workflow Basics