DevOps · Infrastructure as Code (IaC) · Git Series – Part 9
Git doesn’t just track your code — it tracks every change, by whom, when, and why.
Understanding Git history lets you:
- Debug issues quickly
- Track down when bugs were introduced
- Audit IaC changes & deployments
- Recover lost commits
- Navigate branches and merges
- Understand project evolution
- Safely revert or fix mistakes
In this chapter, you’ll master:
git log(full guide)- Pretty logs & visual graphs
git diff(unstaged, staged, committed)git blamefor line-by-line historygit reflog(Git’s secret time machine)- Searching history
- Filtering commits
- Inspecting merges, branches & files over time
Table of Contents
1. Understanding Git History
Git history is a chain of commits, each pointing to a snapshot of your files.
Every commit has:
- Hash (ID)
- Author
- Timestamp
- Commit message
- Parent commit reference
- Snapshot of staged data
This creates a Directed Acyclic Graph (DAG), allowing Git to travel through time.
2. Viewing Commit Logs
Git history starts with the classic:
Basic log
git logShows full details:
- Commit hash
- Author
- Date
- Message
3. Simplifying Log Output
One-line view
git log --oneline
Graph view (tree structure)
git log --graph --onelineDecorate with branch names
git log --graph --oneline --decorateAll branches
git log --oneline --graph --decorate --allThis visualizes the entire repository.
4. Pretty Log Formats (Must Know)
Custom formatted log
git log --pretty=format:"%h - %an (%ar): %s"Common placeholders:
| Placeholder | Meaning |
|---|---|
%h | Short hash |
%an | Author name |
%ar | Relative date (2 days ago) |
%s | Commit message |
5. Filtering Log Output
Show commits by a specific author
git log --author="Ramesh"Search commits by message
git log --grep="fix"Show commits affecting a specific file
git log -- <file>Example:
git log -p -- README.mdLimit number of commits
git log -n 56. Viewing Differences (git diff)
git diff is essential for understanding file changes.
6.1 See unstaged changes
git diffShows what changed but is not staged.
6.2 See staged changes
git diff --cachedUsed before committing.
6.3 Compare working directory with last commit
git diff HEAD6.4 Diff between commits
git diff <commit1> <commit2>Example:
git diff abc123 def4566.5 Diff a single file across commits
git diff <hash1> <hash2> -- file.txt7. Git Blame – See Who Changed Each Line
Used to identify:
- Bug introducer
- Last editor
- Ownership history
- Problematic modifications
Basic blame:
git blame file.txtBlame with date:
git blame -p file.txtBlame a specific commit range:
git blame <commit>^ <commit> -- file.txtBlame is extremely helpful in auditing IaC (Terraform, Ansible), especially for production misconfigurations.
8. Git Reflog – Git’s Time Machine (VERY IMPORTANT)
git reflog logs every action, even those not in normal history.
This is how you recover:
- Lost commits
- Deleted branches
- Hard resets
- Rebased commits
Show full reflog:
git reflogOutput:
a8c9e3f HEAD@{0}: commit: fix deployment issue
7bd92ca HEAD@{1}: checkout: moving from main to hotfix
e1b44dd HEAD@{2}: commit (amend): update api routes9. Recovering Deleted Commits
After accidental reset:
git reflog
git checkout <lost-hash>Restore as branch:
git branch restore-point <lost-hash>Reflog is the most powerful recovery mechanism in Git.
10. Searching Within History
Search commit messages
git log --grep="database"Search history by code content (“who introduced this line?”)
git log -S"variable_name"Search by regular expression
git log -G"regex_pattern"11. Inspecting Merge History
See merge commits only:
git log --mergesShow commits without merges:
git log --no-merges12. Viewing File History Over Time
Who touched this file?
git log -- file.txtView file content at a specific commit
git show <hash>:file.txt13. Branch-Specific History
Show commits only in the current branch:
git log mainShow commits not merged into main:
git log main..featureShow commits in main not in feature:
git log feature..main14. Delete Mistaken Commits from History (Local)
Use interactive rebase:
git rebase -i HEAD~5You can:
- reorder commits
- edit commits
- squash commits
- drop commits
⚠️ Never rewrite history on shared branches.
15. Visual History Tools (Optional but Excellent)
From terminal:
gitk
In VS Code:
- Source Control → View File History
- Extensions like GitLens
GitLens offers:
- blame annotations
- file history viewer
- commit details
- timeline view
16. Real DevOps Use Case Examples
1. Debug who changed a Terraform variable
git blame variables.tf
2. Find when a CI/CD failure started
git log --grep="CI"
3. See history of ansible inventory
git log -- inventory/hosts
4. Recover lost IaC changes after faulty rebase
git reflog
git checkout <hash>
5. Compare two releases
git diff v1.0.0 v1.1.0
17. Git History Summary Table
| Action | Command |
|---|---|
| Basic log | git log |
| One-line log | git log --oneline |
| Graph | git log --graph --oneline --all |
| Staged diff | git diff --cached |
| Unstaged diff | git diff |
| Diff commits | git diff <hash1> <hash2> |
| Blame file | git blame file.txt |
| Show all reflog | git reflog |
| Undo via reflog | git checkout <hash> |
| Search by message | git log --grep="msg" |
| Search by code | git log -S"text" |
| Show file at commit | git show <hash>:file.txt |
18. What’s Next?
You’ve now learned how to:
✔ View history using log & graph
✔ Understand diffs
✔ Trace code lines with blame
✔ Recover lost commits using reflog
✔ Audit IaC changes
✔ Search & filter commits
✔ Travel through Git’s entire timeline
Next chapter:
➡️ Part 10 – Git Help: Getting Help, Documentation, and Learning Commands Efficiently
You’ll learn all Git help commands, usage patterns, official docs, examples, and how to explore any Git feature without external websites.