Git Workflow Basics: Three States for DevOps
📌 DevOps Core Concept
Understanding Git's three states (Working Directory → Staging Area → Repository) is fundamental to managing infrastructure code, configuration files, and deployment scripts efficiently.
Working Directory
Where you edit files (Terraform, Ansible, Docker, etc.)
terraform/
ansible/
docker-compose.yml
Staging Area
Preview area for changes before committing
git add docker-compose.yml
# Changes ready to commit
Repository
Permanent storage of commits (local .git folder)
# Commits stored here
.git/
Understanding Git's Three States
1. Working Directory (Your Files)
This is where you actually work with files. In DevOps, these are your:
- Terraform configuration files (.tf files)
- Ansible playbooks (.yml files)
- Dockerfiles and docker-compose.yml
- Kubernetes manifests (.yaml files)
- CI/CD pipeline scripts (Jenkinsfile, .gitlab-ci.yml)
- Configuration files and scripts
infrastructure/
├── terraform/
├── ansible/
├── kubernetes/
├── docker/
└── scripts/
# These are all in Working Directory
2. Staging Area (Index) - Git's Unique Feature
The staging area is like a "preview" or "shopping cart" for your changes. You decide which changes go into the next commit.
🔧 DevOps Example: Selective Staging
You modified 3 files but only want to commit 2 related changes:
# Modified: terraform/main.tf, ansible/nginx.yml, README.md
# Stage only infrastructure changes
git add terraform/main.tf
git add ansible/nginx.yml
# README.md stays in Working Directory
# Commit will only include staged files
3. Repository (.git directory)
This is where Git stores all commits permanently. Each commit is a snapshot of your entire project at a point in time.
git commit -m "Update infrastructure: Add load balancer config"
# This creates a permanent commit in the repository
# Commit hash: abc123def456...
🎯 Git Workflow Visualization
Click each step to see the command:
Working Directory
Edit infrastructure files
Click to see examplegit add
Stage selected changes
Click to see exampleStaging Area
Changes ready to commit
Click to see examplegit commit
Create permanent snapshot
Click to see exampleRepository
Commits stored permanently
Click to see exampleComplete Workflow Example for DevOps
📋 Infrastructure Change Workflow
Edit Terraform configuration for new VPC
# Add VPC configuration
See what changed in your infrastructure
# Shows: terraform/vpc.tf (modified)
Add specific infrastructure changes to staging
# Moves to Staging Area
Check what's about to be committed
# Preview the VPC configuration
Create permanent snapshot of infrastructure
# Creates commit in local repository
Share infrastructure changes with team/CI/CD
# Triggers CI/CD pipeline
Why Three States Matter for DevOps
| State | DevOps Purpose | Common Commands | Best Practices |
|---|---|---|---|
| Working Directory | Edit infrastructure code, test configurations | vim, nano, IDE edits |
Test changes locally before staging |
| Staging Area | Review and organize changes before committing | git add, git reset |
Stage related changes together |
| Repository | Store approved infrastructure versions | git commit, git log |
Atomic commits with clear messages |
💡 DevOps Staging Strategy
Atomic Commits: Stage and commit related changes together (e.g., all Terraform files for one feature)
Selective Staging: Use git add -p to stage specific parts of a file
Review Before Commit: Always check git diff --staged before committing infrastructure changes
Clear Messages: Use format: component: description (terraform:, ansible:, docker:)
Common Scenarios & Solutions
Scenario 1: Accidentally staged wrong file
git reset HEAD terraform/wrong-file.tf
# File returns to Working Directory, unstaged
Scenario 2: Want to un-modify a file
git checkout -- ansible/playbook.yml
# WARNING: This permanently discards uncommitted changes!
Scenario 3: See differences between states
git diff
# Staging vs Repository
git diff --staged
# Working Directory vs Repository
git diff HEAD
Frequently Asked Questions (FAQ)
The staging area is Git's killer feature! It allows:
1. Selective commits: You modified 5 files but only want to commit 3 related changes
2. Review before commit: See exactly what will be committed
3. Split changes: Separate logical changes into different commits
4. Partial staging: Stage only specific changes within a file (git add -p)
For DevOps: This is crucial when you have multiple infrastructure changes but want to commit them logically.
git add . - Stages new and modified files in current directory and subdirectories
git add -A - Stages ALL changes: new, modified, AND deleted files from entire working tree
DevOps example:
If you deleted a old Terraform file and created a new one:
• git add . stages only the new file
• git add -A stages both the new file AND records the deletion
Usually git add . is sufficient for daily DevOps work.
git status shows both:
• Changes to be committed (staging area)
• Changes not staged for commit (working directory)
More detailed view:
git diff - Shows unstaged changes (WD vs Staging)
git diff --staged - Shows staged changes (Staging vs Repo)
git diff HEAD - Shows all changes (WD vs Repo)
For DevOps: Always check git diff --staged before committing infrastructure changes!
Yes, but not recommended! You can use:
git commit -a -m "message"
This stages ALL modified files and commits in one command.
Why it's bad for DevOps:
1. No chance to review changes before commit
2. Can't commit selectively (commits everything)
3. Easy to commit debug code or temporary changes
4. Hard to create atomic, logical commits
Always use staging area for infrastructure code - it's a safety net!
The staging area becomes empty after a successful commit.
Workflow:
1. You stage changes: git add file1 file2
2. You commit: git commit -m "message"
3. Staging area is now empty
4. Working directory may still have unstaged changes
DevOps tip: After committing infrastructure changes, check git status to see if anything remains unstaged that should be in the next commit.
Use git add -p (patch mode):
git add -p terraform/main.tf
Git will show each change ("hunk") and ask:
• y - stage this hunk
• n - don't stage this hunk
• q - quit
• s - split into smaller hunks
• e - manually edit the hunk
DevOps use case: When your Terraform file has both production and debugging changes, but you only want to commit production changes.
The staging area (also called "index") is stored in the .git/index file.
Location: .git/index in your repository
What's in it:
• List of files that will go into next commit
• SHA-1 hashes of file contents
• File permissions and timestamps
DevOps insight: You generally don't need to touch this file directly, but it's good to know where Git stores staging information. The index is binary, so view it with git ls-files --stage if curious.