This is a card in Dave's Virtual Box of Cards.

Git: Get a file prior to a specific commit

Page created: 2026-06-26

Back to Git.

This could also be titled: "How to undo changes to a specific file after, and including, a specific commit".

Problem: I wanted to get the version of a file prior to a specific commit hash. In my case, I wanted to restore it to the state it was in prior to making the commit changes.

Solution: The command git show [object]:[path] will display the contents of a file from the given "object" (which could be a commit hash or HEAD or another other "refname").

In my case, I wanted the commit just before the one specified by a particular hash, so I used the ^n suffix to specify the 1st parent. Example object: 289bc9e^1.

Example

Put it all together, and this command prints the older version (before commit 289bc9e) of the file foo/bar.c to STDOUT:

g show 289bc9e^1:foo/bar.c

And this one restores the file to that version:

g show 289bc9e^1:foo/bar.c > foo/bar.c

Gotta love source control.