View an older version of a file in Git

Use git show to read or export a historical file, follow renames, compare versions, and restore the right snapshot without switching branches.

Best practices
Illustration of Git branches and version history

Use git show COMMIT:path/to/file to read a file as it existed in a particular commit. This leaves your current branch and working tree alone. Start with the file's history to choose a revision:

git log --oneline -- src/app.js
git show a1b2c3d:src/app.js

Replace the example hash with one from your repository. Paths after the colon are relative to the repository root.

Read, export, or compare a version

# Save the historical contents to a separate file.
git show a1b2c3d:src/app.js > app-old.js

# Show the patch that a commit made to this file.
git show a1b2c3d -- src/app.js

# Read the version three first-parent steps behind HEAD.
git show HEAD~3:src/app.js

# Compare the file at two revisions.
git diff A B -- src/app.js

git show COMMIT:path prints a complete file; git show COMMIT -- path shows the commit's change. Replace A and B with real revisions for the comparison. The > redirection overwrites the output file if it already exists.

For a visual history, use gitk -- src/app.js if installed, or your editor's Git history view. On GitHub, pressing y on a file page turns the URL into a link pinned to the current commit. Blame helps trace individual lines.

Restore the historical contents

git restore --source=a1b2c3d -- src/app.js

This overwrites that working-tree file without moving the branch or staging the result. Review the change, then use git add and git commit if you want to keep it. The older command git checkout a1b2c3d -- src/app.js also updates the index, so it is not identical in its staging behavior.

FAQ

What if the file was renamed?

Use git log --follow --name-status -- src/app.js to trace a single file across detected renames. When inspecting an older commit, use the path that existed at that revision.

What if the file was deleted?

Find the deletion with git log --diff-filter=D -- path/to/file, then read git show DELETION_COMMIT^:path/to/file. The parent belongs to the deletion commit, not to an already selected pre-deletion commit. For a merge, inspect the parents and choose the one that contains the file.

Why are filenames escaped or file contents garbled?

git config core.quotePath false makes many non-ASCII filenames easier to read in command output. It does not convert the file's encoding. For content, use a terminal or editor that understands the bytes stored in the historical file.

See git-show, git-log, git-restore, and GitHub's permanent file links.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting