Viewing File Contents from Any Commit with Git Show
In previous blogs, we learned how to use the tilde (~) and caret (^) operators to move around commits. Normally, if you want to see an old file version, you switch to that commit with git switch.
But switching HEAD every time is not practical.
👉 What if you just want to check the content of a file at a specific commit without moving HEAD?
That’s where git show shines.
Example Setup
We have a repository project10 with one file feature.txt. It has three commits:
- First commit → added line:
first commit - Second commit → added line:
second commit - Third commit → added line:
third commit
History looks like this:
Traditional Way (Switching HEAD)
To check file content at the first commit:
git switch --detach HEAD~2
cat feature.txt
# first commit
Then switch back:
git switch main
This works, but it’s cumbersome if you only want to peek into history.
Using Git Show
Instead of switching, use:
git show <commit-id>:<file-path>
Example — show feature.txt at the second commit:
git show <second-commit-id>:feature.txt
# first commit
# second commit
You can also use commit references:
git show HEAD~1:feature.txt
# first commit
# second commit
Git Show with Branches
You can use branch names instead of commit IDs:
git show main:feature.txt
git show feature:feature.txt
This prints the content of feature.txt as it exists in that branch.
Why Is This Useful?
- No need to move HEAD just to see an old file.
- Works with commit IDs, branch names, HEAD, and even relative refs (
HEAD~1,HEAD^2). - Quick inspection for debugging or verifying changes.
Example Workflow
-
See file at first commit:
git show HEAD~2:feature.txt -
Compare with latest:
git show main:feature.txt -
Inspect another branch:
git show feature:feature.txt
Summary
git show <commit>:<file>prints the file content at a specific commit.- Works with commit IDs,
HEAD,HEAD~n,HEAD^n, or branch names. - Saves you from constantly switching HEAD to older commits.
- Perfect for quick history checks and debugging.
Keep Learning 🚀
👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.
👉 My YouTube Channel – More videos, more fun, and lots of learning!
👉 📺 Watch this topic in video form
Stay curious, keep coding, and let’s make learning fun together! 🎉