1908

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

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
mike
  • 46,876
  • 44
  • 102
  • 112
  • 10
    [How to get a copy of an older version of a file in a git repository?](http://stackoverflow.com/q/14995506/995714) – phuclv Apr 19 '17 at 03:29
  • 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 `, afterwards, `git checkout HEAD` – Homero Esmeraldo Jan 04 '20 at 02:48

11 Answers11

2185

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.

MakotoE
  • 1,814
  • 1
  • 20
  • 39
mipadi
  • 398,885
  • 90
  • 523
  • 479
  • 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 Dec 03 '08 at 20:06
  • And if I just do "git-show path/to/file.c", the command succeeds with no output. – mike Dec 03 '08 at 20:12
  • With what version of git? The other way to do it is with git cat-file given the blob ID (which you can find with ls-tree, but that's the hard way). – Dustin Dec 03 '08 at 22:17
  • 4
    Has to be the complete path, from the top of the git repo – richq Dec 04 '08 at 08:25
  • 24
    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. – Matt McMinn Jul 21 '10 at 14:56
  • 13
    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 Feb 28 '11 at 19:21
  • 9
    If you want to see it in a Vim split so that they scroll together, I wrote a short [blog post](http://readncode.com/blog/view-old-git-version-of-a-file-in-a-vim-split/) showing how to do that. – Flaviu Dec 25 '11 at 06:18
  • mercurial can update the entire repo to previous version.. through update command. any simillar way in git to temporarily switch to previous revision?? – ashishsony Jan 03 '12 at 09:13
  • 1
    @ashishsony: Yes, you can use `git checkout `. – mipadi Jan 03 '12 at 14:52
  • what happens to the changes im doing in the working dir?? any way to keep them intact and get them back once i am back to head revision?Thanks. – ashishsony Jan 04 '12 at 04:22
  • Thanks! What a useful command. I was painfully and inefficiently trying to copy and paste and reformat my old source code from the diff file. – ratsimihah Jul 23 '12 at 23:00
  • you can use git log -- [filename] to look at revisions – Michael Dausmann Feb 16 '14 at 23:32
  • 2
    You can also redirect git show into vim using `(commit):(path to file) | vim -`. I find this much better than using the less/more that is used by default. – laughing_man Mar 06 '14 at 03:57
  • For some reason, the *only* thing that appears to work for me is `git show :file` Everything else just gives the most recent version. – Bobby Jack Nov 24 '14 at 11:45
  • You said "show the 4th last commit of the file", but did you really mean "show the file as it was in the 4th last commit"? – Craig McQueen Apr 15 '15 at 06:19
  • These instructions do not work in my case. It can locate the file on disk but says it isn't in the provided commit; however, I got the commit sha from `git blame`, so it must be in the commit. – weberc2 Jun 10 '16 at 14:08
  • The HEAD:~ syntax does NOT work as expected. It does not show the n'th previous revision of the file src/main.c. It show the content of that file on the n'th previous commit to the current branch. Unless the file src/main.c was modified on every commit in the branch's history, this will not produce the expected result. – Die in Sente Jul 07 '16 at 17:51
  • 1
    `REVISION` can be a commit hash. See my answer: [stackoverflow.com/a/40400259/759452](http://stackoverflow.com/a/40400259/759452) – Adriano Nov 03 '16 at 11:57
  • 1
    `git-show` is great. Can I toggle on line numbers when using it on command line? – KFL Jan 11 '17 at 07:18
  • 2
    To add onto the answer - git-show doesn't give you line numbers. If you want line numbers, try `git show : | less -N` – KFL Jan 11 '17 at 07:20
  • 2
    Instead of having to provide the path from the root of the repo, you can use a relative path if you start it with a `.` for example: `git show HEAD:./subdir/main.c > foo`. This works in recent versions of git (tested in git 2.8.3 on CentOS 7) – Eponymous Sep 06 '17 at 15:53
  • @MichaelDausmann, I have added a series of patches to a file using `git add -p`. Is there a way to see the entire file as it is in the index, before I commit it? – alpha_989 Apr 30 '18 at 18:08
  • Does not work, I get `fatal: Invalid object name 'REVISION'.` – Black Jul 14 '18 at 16:10
  • @Black: 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). – mipadi Jul 16 '18 at 22:24
  • @mipadi, does not work I tried : "git show 717fe60db378e7052247fa5de33e50bce59e2e85:"app\Models\Selenium.php" But there is no output – Black Jul 17 '18 at 08:27
  • @Black: Is that a path in your repository? A lot of things could be going wrong here. – mipadi Jul 18 '18 at 20:06
  • Did not want to upvote this answer as it now has 1337 upvotes. Did it anyway :) – Michel Aug 10 '18 at 09:12
  • This works for me. I get the output in a terminal. Is there a way to send this to a text file? – Diego Oct 23 '19 at 14:00
  • 1
    @Diego: Redirect the output to a file: `git show REVISION:path/to/file > path/to/file`. – mipadi Oct 24 '19 at 04:06
  • Using git for years yet still I have to look this up every time, anyone have a mnemonic for it? The obvious `git show -- ` gives a diff, whilst `git show --help` is overly verbose. Then I have to come back here. There's a reason it's called `git`. – Ed Randall Mar 24 '22 at 07:22
  • Doesn't work. Doesn't go to stdout. Instead it puts me in less. Diego has the answer. – gr5 Feb 07 '23 at 20:24
  • 1
    @gr5: Pass `--no-pager` to Git if you don't want to use `less`. (This works with any Git command.) – mipadi Feb 08 '23 at 21:30
297

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
Jim Hunziker
  • 14,111
  • 8
  • 58
  • 64
131

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.
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Trausti Kristjansson
  • 2,974
  • 2
  • 16
  • 11
  • 8
    This also works with [tig](http://blogs.atlassian.com/2013/05/git-tig/), which is a curses git repo viewer. – Matthew G May 13 '13 at 05: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 Dec 04 '13 at 02:46
  • Note this only seems to work if you start from the root of your git repository. – Marc Mar 23 '16 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. – Christian.D Jul 06 '16 at 07:42
  • 1
    `sudo apt install gitk` for ubuntu – run_the_race Feb 04 '22 at 15:40
112

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.

Community
  • 1
  • 1
Adriano
  • 19,463
  • 19
  • 103
  • 140
54

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

Community
  • 1
  • 1
Ijas Ameenudeen
  • 9,069
  • 3
  • 41
  • 54
52

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

sachin_ur
  • 2,375
  • 14
  • 27
  • 25
    Those commands show the differences with the current version for me but not show the entire file. – Jean Paul Jan 21 '19 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. – Mikko Rantalainen Jun 16 '21 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 Mar 05 '22 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.

sanbor
  • 1,264
  • 15
  • 20
7

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

2240
  • 1,547
  • 2
  • 12
  • 30
Andrew_STOP_RU_WAR_IN_UA
  • 9,318
  • 5
  • 65
  • 101
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

Brad Parks
  • 66,836
  • 64
  • 257
  • 336
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.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
0

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!

Victor Schröder
  • 6,738
  • 2
  • 42
  • 45