How can I view an old version of a file with Git?

GitGit History-Graph

Git Problem Overview


Is there a command in Git to see (either dumped to stdout, or in $PAGER or $EDITOR) a particular version of a particular file?

Git Solutions


Solution 1 - Git

You can use git show with a path from the root of the repository (./ or ../ for relative pathing):

$ git show REVISION:path/to/file

Replace REVISION with your actual revision (could be a Git commit SHA, a tag name, a branch name, a relative commit name, or any other way of identifying a commit in Git)

For example, to view the version of file <repository-root>/src/main.c from 4 commits ago, use:

$ git show HEAD~4:src/main.c

Git for Windows requires forward slashes even in paths relative to the current directory. For more information, check out the man page for git-show.

Solution 2 - Git

Doing this by date looks like this if the commit happened within the last 90 days:

git show HEAD@{2013-02-25}:./fileInCurrentDirectory.txt

Note that HEAD@{2013-02-25} means "where HEAD was on 2013-02-25" in this repository (using the reflog), not "the last commit before 2013-02-25 in this branch in history".

This is important! It means that, by default, this method only works for history within the last 90 days. Otherwise, you need to do this:

git show $(git rev-list -1 --before="2013-02-26" HEAD):./fileInCurrentDirectory.txt

Solution 3 - Git

If you like GUIs, you can use gitk:

  1. start gitk with:

     gitk /path/to/file
    
  2. Choose the revision in the top part of the screen, e.g. by description or date. By default, the lower part of the screen shows the diff for that revision, (corresponding to the "patch" radio button).

  3. To see the file for the selected revision:

  • Click on the "tree" radio button. This will show the root of the file tree at that revision.
  • Drill down to your file.

Solution 4 - Git

You can also specify a commit hash (often also called commit ID) with the git show command.


In a nutshell

git show <commitHash>:/path/to/file


Step by step
  1. Show the log of all the changes for a given file with git log /path/to/file
  2. In the list of changes shown, it shows the commit hash such as commit 06c98... (06c98... being the commit hash)
  3. Copy the commit hash
  4. Run the command git show <commitHash>:/path/to/file using the commit hashof step 3 & the path/to/file of step 1.

Note: adding the ./ when specifying a relative path seems important, i.e. git show b2f8be577166577c59b55e11cfff1404baf63a84:./flight-simulation/src/main/components/nav-horiz.html.

Solution 5 - Git

In addition to Jim Hunziker's answer,

you can export the file from the revision as,

git show HEAD@{2013-02-25}:./fileInCurrentDirectory.txt > old_fileInCurrentDirectory.txt

Hope this helps :)

Solution 6 - Git

To quickly see the differences with older revisions of a file: > git show -1 filename.txt > to compare against the last revision of file

> git show -2 filename.txt > to compare against the 2nd last revision

> git show -3 fielname.txt > to compare against the last 3rd last revision

Solution 7 - Git

git log -p will show you not just the commit logs but also the diff of each commit (except merge commits). Then you can press /, enter filename and press enter. Press n or p to go to the next/previous occurrence. This way you will not just see the changes in the file but also the commit information.

Solution 8 - Git

WAY 1: (I prefer this way, no ability to lose uncommitted data)

  1. Find commit id with: git reflog

  2. List files from commit git diff-tree --no-commit-id --name-only -r <commitHash>

    Example:

    > git diff-tree --no-commit-id --name-only -r d2f9ba4
    > d2f9ba4 is commit id from step 1.

  3. Open required file with following command:

    git show <commitHash>:/path/to/file

    Example:

    > git show d2f9ba4:Src/Ext/MoreSwiftUI/ListCustom.swift
    > Src/... is file path from step 2.


WAY 2: (Ability to lose uncommitted data)

  1. Find commit id with: git reflog

  2. Make hard reset to this commit: git reset --hard %commit ID%

    Example:

    > git reset --hard c14809fa

  3. Make necessary changes and do a new commit into required branch

Solution 9 - Git

You can use a script like this to dump all the versions of a file to separate files:

e.g.

git_dump_all_versions_of_a_file.sh path/to/somefile.txt

Get the script here as an answer to another similar question

Solution 10 - Git

Helper to fetch multiple files from a given revision

When trying to resolve merge conflicts, this helper is very useful:

#!/usr/bin/env python3

import argparse
import os
import subprocess

parser = argparse.ArgumentParser()
parser.add_argument('revision')
parser.add_argument('files', nargs='+')
args = parser.parse_args()
toplevel = subprocess.check_output(['git', 'rev-parse', '--show-toplevel']).rstrip().decode()
for path in args.files:
    file_relative = os.path.relpath(os.path.abspath(path), toplevel)
    base, ext = os.path.splitext(path)
    new_path = base + '.old' + ext
    with open(new_path, 'w') as f:
        subprocess.call(['git', 'show', '{}:./{}'.format(args.revision, path)], stdout=f)

GitHub upstream.

Usage:

git-show-save other-branch file1.c path/to/file2.cpp

Outcome: the following contain the alternate versions of the files:

file1.old.c
path/to/file2.old.cpp

This way, you keep the file extension so your editor won't complain, and can easily find the old file just next to the newer one.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmikeView Question on Stackoverflow
Solution 1 - GitmipadiView Answer on Stackoverflow
Solution 2 - GitJim HunzikerView Answer on Stackoverflow
Solution 3 - GitTrausti KristjanssonView Answer on Stackoverflow
Solution 4 - GitAdrien BeView Answer on Stackoverflow
Solution 5 - GitIjas AmeenudeenView Answer on Stackoverflow
Solution 6 - Gitsachin_urView Answer on Stackoverflow
Solution 7 - GitsanborView Answer on Stackoverflow
Solution 8 - GitAndrew____Pls_Support_UkraineView Answer on Stackoverflow
Solution 9 - GitBrad ParksView Answer on Stackoverflow
Solution 10 - GitCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow