Git History – Logs, Graphs, Blame, Diffs & Time Travel

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 blame for line-by-line history
  • git 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
  • Email
  • 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 log

Shows full details:

  • Commit hash
  • Author
  • Date
  • Message

3. Simplifying Log Output

One-line view

git log --oneline

Graph view (tree structure)

git log --graph --oneline

Decorate with branch names

git log --graph --oneline --decorate

All branches

git log --oneline --graph --decorate --all

This visualizes the entire repository.


4. Pretty Log Formats (Must Know)

Custom formatted log

git log --pretty=format:"%h - %an (%ar): %s"

Common placeholders:

PlaceholderMeaning
%hShort hash
%anAuthor name
%arRelative date (2 days ago)
%sCommit 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.md

Limit number of commits

git log -n 5

6. Viewing Differences (git diff)

git diff is essential for understanding file changes.


6.1 See unstaged changes

git diff

Shows what changed but is not staged.


6.2 See staged changes

git diff --cached

Used before committing.


6.3 Compare working directory with last commit

git diff HEAD

6.4 Diff between commits

git diff <commit1> <commit2>

Example:

git diff abc123 def456

6.5 Diff a single file across commits

git diff <hash1> <hash2> -- file.txt

7. Git Blame – See Who Changed Each Line

Used to identify:

  • Bug introducer
  • Last editor
  • Ownership history
  • Problematic modifications

Basic blame:

git blame file.txt

Blame with date:

git blame -p file.txt

Blame a specific commit range:

git blame <commit>^ <commit> -- file.txt

Blame 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 reflog

Output:

a8c9e3f HEAD@{0}: commit: fix deployment issue
7bd92ca HEAD@{1}: checkout: moving from main to hotfix
e1b44dd HEAD@{2}: commit (amend): update api routes

9. 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 --merges

Show commits without merges:

git log --no-merges

12. Viewing File History Over Time

Who touched this file?

git log -- file.txt

View file content at a specific commit

git show <hash>:file.txt

13. Branch-Specific History

Show commits only in the current branch:

git log main

Show commits not merged into main:

git log main..feature

Show commits in main not in feature:

git log feature..main

14. Delete Mistaken Commits from History (Local)

Use interactive rebase:

git rebase -i HEAD~5

You 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 &lt;hash>

5. Compare two releases

git diff v1.0.0 v1.1.0

17. Git History Summary Table

ActionCommand
Basic loggit log
One-line loggit log --oneline
Graphgit log --graph --oneline --all
Staged diffgit diff --cached
Unstaged diffgit diff
Diff commitsgit diff <hash1> <hash2>
Blame filegit blame file.txt
Show all refloggit reflog
Undo via refloggit checkout <hash>
Search by messagegit log --grep="msg"
Search by codegit log -S"text"
Show file at commitgit 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.

Leave a Reply

Your email address will not be published. Required fields are marked *