Examining History
The power of Git lies in its ability to remember every change made to your project. Unlike traditional file backups, Git doesn't just store files; it stores a Directed Acyclic Graph (DAG) of snapshots. In this chapter, we'll learn how to navigate this timeline with surgical precision.
1. The Standard Log: git log
The git log command is your primary window into the past. By default, it displays commits in reverse chronological order.
Anatomy of a Log Entry
Every time you run git log, you see a block of text for each commit. Understanding these fields is fundamental:
- Commit Hash (SHA-1): A unique 40-character identifier. You only need the first 7 characters to reference it in most commands.
- Author: The person who originally wrote the code.
- Date: When the commit was created.
- Commit Message: The summary of why the change was made.
2. Formatting the Timeline
Professional Git users rarely use the default log because it's too verbose for large projects. Instead, they use flags to "compress" the history.
The Power User's View: --oneline --graph --decorate
This combination is so common that many developers alias it to a shorter command. It provides a visual map of how branches diverge and merge.
--oneline: Squashes the entry to a single line.--graph: Draws an ASCII-art graph representing the branch structure.--decorate: Shows where branch pointers (likemain) and tags are currently located.
3. Filtering History
Git allows you to filter the log to find exactly what you're looking for without scrolling through thousands of entries.
By Time and Quantity
git log -n 5: Show only the last 5 commits.git log --since="2 weeks ago": Show commits from the last 14 days.git log --until="2023-01-01": Show commits made before the new year.
By Content and Author
git log --author="John": See only commits made by John.git log --grep="bugfix": Search commit messages for specific keywords.git log -S "function_name": The "Pickaxe" search. Finds commits where the number of occurrences of a string changed (i.e., when a function was added or removed).
By Path
To see the history of a specific file or directory:
git log -- path/to/file.txt
4. Surgical Inspection
Sometimes you need to look closer at a specific moment in time.
git show: The Deep Dive
While git log tells you a commit exists, git show tells you exactly what happened inside it. It displays the commit metadata and a patch (diff) of the changes.
git show <hash>
git blame: The Accountability Tool
If you want to know who modified each line of a file and when, use git blame. It displays the file content with the commit hash and author next to every line.
git blame index.html
5. The Safety Net: git reflog
What happens if you accidentally delete a branch or perform a bad rebase? The commits might disappear from git log, but they are still in Git's database for about 30 days.
The Reflog records every time your HEAD pointer moves (switching branches, committing, rebasing). It is the ultimate "undo" history.
git reflog
6. Summary: Choosing Your View
| Command | Best For... |
|---|---|
git log --oneline | Quick overview of recent work. |
git log --graph --all | Visualizing branch relationships. |
git log --stat | Seeing which files were impacted. |
git show | Reviewing code changes in a specific commit. |
git blame | Understanding the history of a specific file line-by-line. |
In the next chapter, we'll dive deeper into comparing different versions of your project using git diff.