Git's history is your project's memory. Every commit creates a snapshot, and you can travel through these snapshots to see what changed, when, and why.
Start with the basic log command to see your commit history:
git log
This shows your commits in reverse chronological order. Each entry displays the commit hash (that long string of letters and numbers), author, date, and commit message. The hash is Git's unique fingerprint for that exact state of your project.
Most of the time, the default log output is cluttered. Add --oneline to see just the essentials:
git log --oneline
Now each commit fits on one line: the short hash and commit message. Much cleaner for scanning through changes.
See what actually changed
The log tells you when changes happened. To see what changed, add -p for patch view:
git log -p
This shows the full diff for each commit. Every line added appears with a +, every line removed with a -. It's detailed but can be overwhelming for large changes.
For a middle ground, use --stat to see which files changed and how much:
git log --stat
This shows filenames with a visual representation of changes. Three files modified, twenty lines added, five removed. Quick and informative.
Navigate to specific points in time
You don't always want to see the entire history. Limit the output with these options:
git log --oneline -5
Shows only the last 5 commits. Replace 5 with any number.
git log --since="2 weeks ago"
Shows commits from the last two weeks. You can use "yesterday", "1 month ago", or specific dates like "2024-01-15".
git log --oneline main.py
Shows only commits that modified main.py. Replace with any filename or folder path.
Jump between versions
Every commit has a unique hash. You can reference any commit by its hash, or use shortcuts for recent commits:
HEADpoints to your current commitHEAD^is the previous commit (HEAD's parent)HEAD^^is two commits agoHEAD~3is three commits ago
To see what your project looked like at a specific commit:
git show HEAD~2
This displays the commit details and changes for two commits ago. Use any hash instead of HEAD~2 to see that specific commit.
Want to see a specific file from a previous commit? Use this format:
git show HEAD~2:main.py
This shows how main.py looked two commits ago without changing your current version.
Compare different versions
The real power comes from comparing versions. See what changed between two points:
git diff HEAD~3 HEAD
Shows all changes between three commits ago and now. The output uses the same + and - format as log patches.
Compare specific files between versions:
git diff HEAD~2 HEAD main.py
This focuses on just how main.py changed in the last two commits.
Use the visual history browser
If you installed Git with graphics support, try gitk:
gitk
This opens a visual browser showing your commit tree, changes, and files. Much easier for understanding complex histories with multiple branches. Some systems might not have this installed, but it's worth trying.
Git's history commands let you understand your project's evolution. The log shows the timeline, show reveals details, and diff compares changes. Use these tools when you need to understand what went wrong, find when a bug appeared, or remember why you made certain changes.
Your project's history is now readable and navigable. Next, you'll learn about branches, which let you work on different features without affecting your main code.