How Can I View an Old Version of a File with Git?

Better Stack Team
Updated on July 25, 2024

To view an old version of a file with Git, you have several options depending on whether you want to simply view it, compare it with other versions, or temporarily restore it. Here are the most common methods:

Method 1: Using git show

The git show command displays information about an object (such as a commit or a file in a commit).

  1. Find the Commit SHA:

    • Use git log to find the commit SHA that contains the version of the file you want to view.

       
      git log
      
  2. Show the File:

    • Use git show to view the file's content at that commit.

       
      git show <commit_SHA>:<path_to_file>
      

    For example:

     
    git show a1b2c3d4:path/to/file.txt
    

Method 2: Using git log with p Option

The git log -p command shows the commit history along with the differences introduced in each commit.

  1. View the Commit History for a Specific File:

     
    git log -p path/to/file.txt
    
  2. Navigate Through the Commits:

    • You can see the changes made to the file in each commit, including the content of the file at each point in time.

Method 3: Using git checkout

You can temporarily check out an old version of a file. Be careful with this method as it changes your working directory state.

  1. Find the Commit SHA:

    • Use git log to find the commit SHA that contains the version of the file you want to view.

       
      git log
      
  2. Check Out the Old Version of the File:

    • Check out the file from a specific commit.

       
      git checkout <commit_SHA> -- <path_to_file>
      

    For example:

     
    git checkout a1b2c3d4 -- path/to/file.txt
    
  3. Revert the File Back to the Latest Version:

    • After viewing or working with the old version, you can revert the file to its latest state.

       
      git checkout -- path/to/file.tx
      

Method 4: Using git diff

If you want to compare different versions of a file, use git diff.

  1. Find the Commit SHAs:

    • Use git log to find the commit SHAs you want to compare.

       
      git log
      
  2. Compare the File Between Two Commits:

    • Use git diff to see the differences between the two commits for a specific file.

       
      git diff <commit_SHA1> <commit_SHA2> -- <path_to_file>
      

    For example:

     
    git diff a1b2c3d4 e5f6g7h8 -- path/to/file.txt
    

Example Workflow

  1. View an Old Version Using git show:

     
    git log
    

    Identify the commit SHA, then:

     
    git show a1b2c3d4:path/to/file.txt
    
  2. Check Out an Old Version Temporarily:

     
    git log
    

    Identify the commit SHA, then:

     
    git checkout a1b2c3d4 -- path/to/file.txt
    

    When done, revert to the latest version:

     
    git checkout -- path/to/file.txt
    

By following these methods, you can effectively view and manage old versions of files in a Git repository.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.