Inspecting Commits and Files: git show, diff-tree and Files at Any Revision
Look at any single commit, tag or file version: git show for a commit's message and changes, showing only names or stats, a file as it was in any commit or in the staging area, listing a commit's files with ls-tree, saving or restoring an old version, merge and tag output, and diff-tree for scripts.
kvi5hnuCode Sprout team member7 min readOn this page
git log lists many commits. When you want to look closely at one thing, a commit, a tag, or a file as it was at some point, the tool is git show. This article covers its common forms, plus the commands for listing and recovering files from any commit.
git show for a commit
With no arguments, git show shows HEAD, the last commit: its ID, author, date, message and the diff of what it changed. Give any commit to see that one instead: a short ID, a branch name, a tag, or a relative name such as HEAD~2, two commits before HEAD. (@ is shorthand for HEAD, so @~2 is the same commit.)
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md
git add . && git commit -q -m "Add README"
printf 'Ridge loop: 8 km\n' > trails.txt
git add . && git commit -q -m "Add ridge loop"
printf 'Lake path: 5 km\n' >> trails.txt
echo "Notes and maps." >> README.md
git commit -q -am "Add lake path and describe the guide"
echo "=== git show (HEAD)"
git show
echo "=== HEAD~1, as a summary of changed files"
git show --stat HEAD~1
echo "=== names only, without the commit header"
git show --name-only --format= HEAD
echo "=== just the commit header, no diff"
git show -s --format='%h %an %s' HEAD~2
=== git show (HEAD)
commit aca6763801355989181241d85fdbdacc7f7735d9
Author: Sam Lee <sam@example.com>
Date: Mon Mar 2 10:00:00 2026 +0000
Add lake path and describe the guide
diff --git a/README.md b/README.md
index fc9f508..e01daa9 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
# Trail guide
+Notes and maps.
diff --git a/trails.txt b/trails.txt
index e4e52a0..8a303ba 100644
--- a/trails.txt
+++ b/trails.txt
@@ -1 +1,2 @@
Ridge loop: 8 km
+Lake path: 5 km
=== HEAD~1, as a summary of changed files
commit d43b556f86d82f14afaea95c03c32009cd8811fe
Author: Sam Lee <sam@example.com>
Date: Mon Mar 2 10:00:00 2026 +0000
Add ridge loop
trails.txt | 1 +
1 file changed, 1 insertion(+)
=== names only, without the commit header
README.md
trails.txt
=== just the commit header, no diff
61da1f1 Sam Lee Add README
git show accepts the same output options as git log and git diff: --stat, --name-only, --name-status, --format, --word-diff, -w, paths to limit the diff, and so on. -s (--no-patch) suppresses the diff, and an empty --format= suppresses the header. git show HEAD -- README.md shows only that file's part of the commit.
A file as it was in any commit
The syntax <revision>:<path> names a file as stored in a commit. git show prints it:
git init -q trail-guide && cd trail-guide
mkdir trails
echo "Ridge loop: 8 km" > trails/ridge.txt
echo "# Trail guide" > README.md
git add . && git commit -q -m "Add ridge loop"
echo "Ridge loop: 8.5 km" > trails/ridge.txt
git commit -q -am "Correct ridge distance"
echo "Ridge loop: 8.5 km, gravel" > trails/ridge.txt
git add trails/ridge.txt
echo "Ridge loop: 8.5 km, gravel, shaded" > trails/ridge.txt
echo "=== first commit"
git show HEAD~1:trails/ridge.txt
echo "=== last commit"
git show HEAD:trails/ridge.txt
echo "=== staging area (nothing before the colon)"
git show :trails/ridge.txt
echo "=== working tree"
cat trails/ridge.txt
echo "=== two files at once"
git show HEAD:README.md HEAD:trails/ridge.txt
echo "=== a folder in a commit"
git show HEAD:trails
=== first commit
Ridge loop: 8 km
=== last commit
Ridge loop: 8.5 km
=== staging area (nothing before the colon)
Ridge loop: 8.5 km, gravel
=== working tree
Ridge loop: 8.5 km, gravel, shaded
=== two files at once
# Trail guide
Ridge loop: 8.5 km
=== a folder in a commit
tree HEAD:trails
ridge.txt
HEAD~1:trails/ridge.txtis the file in the commit beforeHEAD. Any revision works:main:path,v1.0:path,a1b2c3d:path.- The path is from the top of the repository, even when you run the command in a subfolder. Start it with
./, as inHEAD:./ridge.txt, to mean "relative to where I am". :path, with no revision, is the staged version. During a merge conflict,:1:path,:2:pathand:3:pathare the common ancestor's, your branch's and the other branch's versions (article 13).- A folder, such as
HEAD:trails, lists its contents.HEAD:alone lists the top folder. - Several objects at once are shown one after another.
Listing every file in a commit
git ls-tree lists a commit's files with their modes and object IDs. -r recurses into folders and --name-only prints just the paths:
git init -q trail-guide && cd trail-guide
mkdir -p trails/north
echo "# Trail guide" > README.md
echo "Ridge loop: 8 km" > trails/ridge.txt
echo "Pine ridge: 7 km" > trails/north/pine.txt
git add . && git commit -q -m "Start project"
echo "=== top level"
git ls-tree HEAD
echo "=== every file, recursively"
git ls-tree -r --name-only HEAD
echo "=== with sizes"
git ls-tree -r -l HEAD trails/
=== top level
100644 blob fc9f508347d28b40eb3d7526f37ec1a43f02f8bf README.md
040000 tree bea949a364251126a1889ed7b50acaa57f44aa3a trails
=== every file, recursively
README.md
trails/north/pine.txt
trails/ridge.txt
=== with sizes
100644 blob e98f2a33b390df3c62c9940e9aecd5814e4c418a 17 trails/north/pine.txt
100644 blob e4e52a05617d5ccc302695b22864aea0c62cb606 17 trails/ridge.txt
The top level shows blob entries for files and tree entries for folders: the objects Git stores, covered in article 48. -l adds each file's size in bytes. Unlike git ls-files, which lists the staging area, ls-tree reads any commit, without checking it out.
Getting an old version back
To recover a file as it was in an earlier commit, either write it under another name, or restore it in place:
git show HEAD~3:config.ini > config.old.ini # save a copy for comparison
git restore --source=HEAD~3 config.ini # replace the working-tree file
git restore --source=HEAD~3 --staged --worktree config.ini # ...and stage it too
git checkout HEAD~3 -- config.ini # the older form: replaces and stages
git restore --source (Git 2.23) changes only the working tree unless you add --staged. The older git checkout <commit> -- <path> updates both the working tree and the staging area. Either way, committing afterwards records the old content as a new change, and history is untouched. Article 24 covers restoring and undoing in depth, including recovering a file deleted in an earlier commit.
Merge commits and tags
A merge commit has two parents. git show on a merge shows a combined diff: only the parts where the merge result differs from both parents, which is usually where conflicts were resolved. A clean merge often shows no diff at all. To see what the merge brought into the branch, compare it with its first parent: git show --first-parent <merge>, or git diff <merge>^1 <merge>. Article 12 covers merges.
An annotated tag (article 38) has its own message, tagger and date. git show v1.0 shows the tag's details first, then the commit it points to:
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md
git add . && git commit -q -m "Add README"
git tag -a v1.0 -m "First public release"
git show --stat v1.0
tag v1.0
Tagger: Sam Lee <sam@example.com>
Date: Mon Mar 2 10:00:00 2026 +0000
First public release
commit 61da1f1bc4c3d2495583c93941ff462706185b77
Author: Sam Lee <sam@example.com>
Date: Mon Mar 2 10:00:00 2026 +0000
Add README
README.md | 1 +
1 file changed, 1 insertion(+)
diff-tree: changed files, for scripts
git diff-tree compares two trees, usually a commit and its parent. It's a plumbing command: its output format is stable, so scripts can rely on it. Porcelain commands like git show may change their output between versions:
git init -q trail-guide && cd trail-guide
mkdir trails
echo "# Trail guide" > README.md
git add . && git commit -q -m "Add README"
echo "Ridge loop: 8 km" > trails/ridge.txt
echo "Notes and maps." >> README.md
git add . && git commit -q -m "Add ridge loop"
echo "=== changed files in HEAD"
git diff-tree --no-commit-id --name-status -r HEAD
echo "=== the first commit needs --root, as it has no parent"
git diff-tree --no-commit-id --name-only -r --root HEAD~1
=== changed files in HEAD
M README.md
A trails/ridge.txt
=== the first commit needs --root, as it has no parent
README.md
-rrecurses into folders. Without it, a changed folder is listed as one entry.--no-commit-idleaves out the commit ID line it prints first.--roottreats the first commit as a change from nothing. Without it, the root commit shows no files.- It also takes
--stat,--numstat,-pand rename detection with-M, likegit diff.
For one-off use, git show --name-status --format= <commit> gives the same list.
Older guides mention git whatchanged, which showed each commit with its changed files. It's deprecated, refuses to run without --i-still-use-this, and is removed in Git 3.0. Use git log --raw or git log --name-status instead.
What's current
git restore --source(Git 2.23) is the clear way to bring back a file from another commit.git checkout <commit> -- <file>still works.git whatchangedis deprecated and removed in Git 3.0.git log --rawshows the same information.git showandgit logon a merge can show how conflicts were resolved with--remerge-diff, which is often clearer than the combined diff (article 13).
Common questions
How do I see a file as it was in another branch, without switching?
git show other-branch:path/to/file. To compare it with your version, use git diff other-branch -- path/to/file.
Why does git show <commit>:file say "exists on disk, but not in <commit>"?
The file exists in your working tree at that path, but not in that commit: it was added later, or had a different name then. Find out with git log --follow --name-status -- file. From a subfolder, the message is different ("path 'src/app.js' exists, but not 'app.js'"), because paths are from the top of the repository: use ./file or the full path.
What's the difference between git show and git log -p -1?
For a normal commit, very little: both show the header and diff. git show also works on tags, trees and files, and shows merges as combined diffs, while git log -p shows no diff for merges by default.
How do I list the files that changed in a commit?
git show --name-only --format= <commit>, or for scripts, git diff-tree --no-commit-id --name-only -r <commit>.
Summary
git showshows a commit's header and diff,HEADby default. It takes--stat,--name-only,-s,--formatand paths.<revision>:<path>names a file in any commit, and:<path>names the staged version. Paths are from the repository's top unless they start with./.git ls-tree -r --name-only <commit>lists every file in a commit.git restore --source=<commit> <file>orgit show <commit>:<file> > copyrecovers old content.git showon an annotated tag shows the tag's message and then the commit. On a merge, it shows a combined diff.git diff-tree --no-commit-id --name-only -r <commit>lists changed files with a stable format for scripts. Add--rootfor the first commit.
Practice questions
What does each of these show: git show HEAD~2, git show HEAD~2:app.js, git show HEAD~2:?
Answer
git show HEAD~2 shows the commit two before HEAD: its header and diff. git show HEAD~2:app.js prints the contents of app.js as stored in that commit. git show HEAD~2: lists the files and folders at the top level of that commit.
You're in the src/ folder and run git show HEAD:app.js, but app.js is in src/. What happens, and how do you fix it?
Answer
Git looks for app.js at the top of the repository, doesn't find it, and reports an error (suggesting HEAD:src/app.js or HEAD:./app.js). Paths after the colon are from the repository's top folder unless they start with ./. Use git show HEAD:src/app.js or git show HEAD:./app.js.
A teammate deleted legacy.css in some commit last month. You need its last content, without restoring it into the project. How?
Answer
Find the commit that deleted it with git log --oneline -1 -- legacy.css (the newest commit touching the path is the deletion). Then show the file from that commit's parent:
git show <deleting-commit>^:legacy.css > legacy-copy.css
<commit>^ is the commit's parent, the last commit in which the file still existed.
Review quiz
Test yourself on Part 2 with the Git Part 2 review quiz.
Previous: Browsing History With git log: Formatting, Graphs, Filtering and Searching
Next: Git Branches: Creating, Switching, Listing, Renaming and Deleting

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.