The Power of the Log
As a project grows, the history becomes a massive database of changes. git log is the query language for this database. Using it effectively can save you hours of debugging.
Formatting the Log
The default git log is verbose. To get a high-level overview, you can customize the output format.
One-line
git log --oneline
This shows just the abbreviated SHA and the commit message.
Decorating
To see where branches and tags are pointing:
git log --oneline --decorate
Graphing
To visualize the branching history in the terminal:
git log --oneline --graph --all
This draws an ASCII art tree of your commit history, showing merges and divergences.
Filtering History
You often want to find specific commits rather than listing everything.
By Time
git log --since="2 weeks ago"
git log --until="2023-01-01"
By Author
git log --author="Linus"
By File
To see only commits that modified a specific file:
git log -- path/to/file.py
By Content (Search)
To search for commits that added or removed a specific string (the “Pickaxe”):
git log -S "functionName"
Inspecting Commits
If git log lists the commits, git show inspects the details of a single object.
git show a1b2c3d
This displays the log message and the diff of what changed in that commit.
Blaming
When you encounter a bug, you often want to know: “Who wrote this line and when?” git blame annotates each line of a file with the commit information.
git blame main.c
Output:
^4a2b3c (Alice 2023-01-01 10:00:00 +0000 1) int main() {
8d7e6f (Bob 2023-01-02 11:00:00 +0000 2) return 0;
^4a2b3c (Alice 2023-01-01 10:00:00 +0000 3) }
In many IDEs, this functionality is integrated as “Git Lens” or “Annotate”.
Reflog: The Safety Net
Git keeps a log of where your HEAD pointer has been, even if you deleted branches or reset commits. This is called the “Reference Log” or reflog.
git reflog
It shows a local history of your actions (switching branches, resetting, committing). It is invaluable for recovering “lost” commits.
Diffing
We’ve used git diff to see working directory changes. But it is much more powerful.
Comparing Branches
To see what is in feature that is not in main:
git diff main..feature
Comparing Commits
To see the difference between two specific points in time:
git diff HEAD~2 HEAD
Which command would you use to find out which commit introduced the string 'TODO: Fix this'?
Visualizing the Graph
# Complete the command to show a decorated, one-line graph of all branches
git log --oneline --decorate --graph --