1981

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

2
  • 10
  • 3
    If you came to this question because you want to check an older version of a binary file (e.g. an image), then better to do a checkout to the old commit, see what you need to see, and then come back to the HEAD. For that, do git checkout <sha1-of-the-commit-you-need>, afterwards, git checkout HEAD Commented Jan 4, 2020 at 2:48

12 Answers 12

2263

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 (a Git commit SHA, tag name, branch name, 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.

35
  • 6
    That doesn't actually seem to work -- did you try it? For "git show HEAD:path/to/file.c", I get an "ambiguous argument" error.
    – mike
    Commented Dec 3, 2008 at 20:06
  • 5
    Has to be the complete path, from the top of the git repo
    – richq
    Commented Dec 4, 2008 at 8:25
  • 25
    If you're on windows, it might be a path separator thing; if I do git show HEAD:dir\subdir\file, I get the anbiguous argument. If I do git show HEAD:dir/subdir/file, it works as expected. Commented Jul 21, 2010 at 14:56
  • 14
    The path you must provide after the : is from the root of the git repository. (this was given below as an answer but I think it was intended as a comment on this answer)
    – Tyler
    Commented Feb 28, 2011 at 19:21
  • 10
    If you want to see it in a Vim split so that they scroll together, I wrote a short blog post showing how to do that.
    – Flaviu
    Commented Dec 25, 2011 at 6:18
304

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
6
  • 5
    This command is useful with master instead of HEAD@{2013-02-25}, if you're on a branch
    – funroll
    Commented Nov 9, 2015 at 13:56
  • 1
    Can you include the time, à la git log --since='2016-04-28 23:59:59 +0100'?
    – dumbledad
    Commented May 3, 2016 at 12:55
  • 15
    The fact this syntax uses the reflog is important and should be highlighted strongly, because the reflog does not contain all commits. See blog.endpoint.com/2014/05/git-checkout-at-specific-date.html Commented Dec 30, 2016 at 4:21
  • 1
    Something which I missed: there cannot be a space after the colon : and before the filename. Commented Jan 11, 2018 at 9:19
  • @AliceHeaton This cannot be stressed enough. (Thanks !) Commented Jan 25, 2021 at 9:37
132

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.
5
  • 9
    This also works with tig, which is a curses git repo viewer.
    – Matthew G
    Commented May 13, 2013 at 5:01
  • 1
    @Paul Slocum: May be because this command is not a conventional command, not the built-in of git. I think this command only work for Windows.
    – Envil
    Commented Dec 4, 2013 at 2:46
  • Note this only seems to work if you start from the root of your git repository.
    – Marc
    Commented Mar 23, 2016 at 16:43
  • If you want to check against a certain revision with gitk you could also use this shortcut: gitk REVISION /path/to/file. This can come in handy when you want to check against a certain version for instance. Commented Jul 6, 2016 at 7:42
  • 1
    sudo apt install gitk for ubuntu Commented Feb 4, 2022 at 15:40
116

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.

3
  • 2
    in case you don't know path to file, use git show <SHA1> --name-only to get it.
    – Tiina
    Commented Oct 11, 2017 at 1:03
  • this command op - even auto completes from memory - tested on a deleted directory... can't get more op than that gg
    – treyBake
    Commented Nov 10, 2017 at 12:55
  • in debian, the addition of ./ does not matter for the pathing.
    – Timo
    Commented Aug 15, 2020 at 12:53
56

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 :)

0
56

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

3
  • 26
    Those commands show the differences with the current version for me but not show the entire file.
    – Jean Paul
    Commented Jan 21, 2019 at 11:31
  • 3
    It's important to notice that this answer matches the question "How to show differences to given file in recent commits?" instead of "How can I view an old version of a file with Git?" the the original question asks. Commented Jun 16, 2021 at 13:44
  • The difference is about the : - double colon - between commit-hash and file the commenters mention about the entire file and diff to another older version.
    – Timo
    Commented Mar 5, 2022 at 14:07
34

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.

2
  • 4
    Looks like git log -pm would also show merge commits.
    – sanbor
    Commented May 6, 2016 at 20:07
  • 8
    You can also run git log -p -- filename.txt to restrain the history to only the desired file.
    – Jean Paul
    Commented Jan 21, 2019 at 12:25
9

WAY 1:

  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
    where 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:

///////////////
/// WARNING: 
/// Ability to lose uncommitted data. 
/// Perform commit or save your uncommited files to stash.
///////////////
  1. Find commit id with: git reflog
  2. Make hard reset to this commit: git reset --hard %commit ID%

Example:

git reset --hard c14809fa

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

WAY 3: ( MacOS, TaoGit - it's free to use )

I prefer this way.

After steps on screenshot below you will have ability to copy all needed data even if commited data is "lost" in commit to detached head

enter image description here

2
  • WARNING: Please be careful with the second method as you will lose all uncommited changes when you do a hard reset! Commented Dec 1, 2020 at 16:25
  • yes, this is ok with hard reset. that's because of reset is"hard" but not "soft". But you need to do hard because of possibilities of conflicts. Commented Dec 1, 2020 at 18:31
4

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

3
  • 1
    git_root, git_log_short and git_log_message_for_commit are missing.
    – mogsie
    Commented Jan 26, 2018 at 14:23
  • Good catch! I double posted this answer to 2 different spots, and just removed this one and linked to the other one, where people told me about this before... thanks @mogsie !
    – Brad Parks
    Commented Jan 26, 2018 at 14:32
  • This script is very useful!
    – XMAN
    Commented Dec 22, 2018 at 3:33
2

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.

3
  • @MickeyPerlstein if you can make achieve the same interface with a better implementation, I'm all ears. Commented Mar 24, 2019 at 16:38
  • maybe i don't understand (and if so, my apologies) but isn't it just : "git show version:./path > new_path " ? Commented Mar 26, 2019 at 11:34
  • @MickeyPerlstein hi, yes, my command generates that CLI, but it loops over multiple files and produces output name from input, so you don't have to type too much. Nothing revolutionary of course, but convenient. Commented Mar 26, 2019 at 21:16
1

None of the previous answers addressed the second possibility mentioned by the OP, which is how to open the results into $EDITOR.

Most editors on the terminal will accept reading from stdin if you pass a single dash - as the filename, which allows piping the output of the git show command to the command you would use to open the editor.

As a Vim user, I'll use it as an example to clarify. You could do the following:

# The reference to a commit, branch, tag, etc
$ REVISION='...' 

$ git show "$REVISION":path/to/file | vim -

One drawback of doing this is that the editor has no good hint of what is the file type you are dealing with and it may have trouble with syntax highlighting, for example. This happens because there is no file extension to look at. From the editor's perspective, it just receives a blob of bytes from stdin.

In Vim, this can be easily solved by explicitly setting the filetype:

$ git show "$REVISION":path/to/file.py | vim -c 'set filetype=python' -

Something very useful is to combine git show with process substitution to compare two historical versions of a file directly using a diff utility (diff, vimdiff, etc). The file may have changed in position a lot inside the Git repository or maybe it was deleted for a while and later recreated. These situations give a hard time to Git to show the diff you want, but the following command does the trick:

$ vimdiff <(git show "$REV_0":path/to/file) <(git show "$REV_1":another/path/to/file)

Nice to find something to add to an almost 15 years old question!

0

You can do it even with GUI:

sudo apt install libcgi-pm-perl gamin
git instaweb --httpd=apache2

Replace apache2 with your www server or install lighttpd previously. After execution of these commands, browser will be opened:

enter image description here

Use tree, diff to achieve what you asked.

See also -- revert whole project without changing work tree.

Not the answer you're looking for? Browse other questions tagged or ask your own question.