Git How to View Old Version of a File (TLDR; ‘git show ID/file-path’)

To view an old version of a file, run the git show command by specifying the revision and the path to the file.

git show REVISION:/file-path


  • REVISION can be the commit SHA, tag name, branch name, relative commit name, or anything similar that identifies a commit in Git
  • file-path is the path to the file relative to the current directory.

As an example, running this command could look something like this:

 $ git show eb66ea0:test2.txt 

This comprehensive guide shows you how to check the older version of a file in your Git project.

Let’s jump into it!

4 Ways to View Old Versions of Files in Git

Git tracks changes to your files as long as you add and commit them. This mechanism allows you to neatly travel back in time to see what your project files looked like in the past.

Let’s see a couple of concrete examples to support understanding.

1. Show Old File with Commit SHA

In Git, each commit has a unique commit ID known as the commit SHA. You can view the commit IDs for example by logging your project’s commit history with git log (and its variations, such as git log –oneline).

The random-looking character-digit combinations on the left-hand side are the commit IDs or the SHAs of the commits.

One convenient and popular way to view the old version of a file in your Git project is by referring to the file in an earlier commit by the commit ID.

git show SHA:/file-path


Let’s take a look at a concrete example of finding the old version of a file using the commit SHA (or ID).

Here’s an example project I have:

And here’s the commit history of the project.

Let’s check the contents of test2.txt file in the 5th commit from the top with the id eb66ea0:

 $ git show eb66ea0:test2.txt 
This is just a test

This shows that after the 5th most recent commit, the test2.txt file had a line of text “This is just a test”.

2. Show Old File with Relative Commit Name

Another way to access commits in Git is by counting the number of commits and referring to them with respect to the HEAD.

This happens with the syntax of HEAD~N, where HEAD is the tip of the current branch (usually, the most recent commit), and N is the number of commits before.

So for example, the 3rd most recent commit is HEAD~2. This is because HEAD refers to the most recent commit. HEAD~1 to the second most recent commit, and HEAD~2 to the third.


Let’s repeat the 1st example.

This time let’s use HEAD instead of the commit SHA to check what test2.txt looked like 5 commits prior.

Because HEAD refers to the most recent commit, we only need to take 4 steps back:

$ git show HEAD~4:test2.txt
This is just a test

3. Show Old File in Other Branch

To see the contents of a file in another branch, you can use the same syntax you would use to see the contents in the current branch.

git show BRANCH:/file-path

Where BRANCH is the name of the branch in which you want to check the file. Notice that you can check any version of the file in the other branch either by referring to the commit SHAs or the number of previous commits.


In my example project, there are three branches: main, feature, and other.

To quickly view the contents of test2.txt 3 commits ago without changing to the other branch, let’s run git show branch-name/file-path as follows:

 $ git show other~3:test2.txt
This is just a test

And to view the most recent version of the file in the other branch:

 $ git show other:test2.txt 
This is just a test
This is actually more than a test

This shows that the file has two lines of text in the other branch.

4. Dealing with Paths

Let’s see one more example that demonstrates using paths.

If you know how OS paths work, you don’t need to check this example. But if you’re new to paths/like/this/one, you should learn about them.

In the previous examples, the path to the file has simply been the name of the file. This has been possible because, in the previous examples, the test2.txt file lived in the root directory of the Git project.

But if there’s a subfolder structure in your project, you cannot just refer to the file by its name. Instead, you need to specify the path to the file that describes in which folder the file lives.


For example, here’s my example project’s new folder structure:

The file lives inside the examples folder which resides in the code folder. Thus, the path to the file is code/examples/

Let’s show the contents of the file in the most recent commit:

 $ git show HEAD:code/examples/   
print('Hello world')


Today you learned how to view an older version of a file in Git.

To take home, Git stores information about versions of project files when you change and commit them. This means you can travel back in time to view what your project files looked like earlier.

To view the older version of your file, you need to use the git show command and specify the revision in which you want to view the file at. You can refer to a revision by using the commit SHA, HEAD, or branch name.

git show REVISION:/file-path

Make sure to get the paths right too! If the file lives in the main directory of your project, its path is just the name of the file. But if the file lives in a subfolder, you need to specify the path to the file via the subfolders.

Thanks for reading. Happy coding!

Read Also

How to Commit Parts of a File

Scroll to Top