Is there a quick git command to see an old version of a file?

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 add -A and git add . command failing
  • Phpstorm Git file colors
  • Configuring PATH globally for SSH remote commands
  • Is there any danger to including functional corrections in git merge commits?
  • Why does git rebase say “fatal: cannot convert etf8 to utf8”?
  • why not deploy code directly from git or bzr repository?
  • Modify file before commit with pre-commit hook
  • Net Start SSHD HELPMSG 3534
  • How can I move a tag on a git branch to a different commit?
  • Resources for using Git with RStudio
  • How to make git repo remember all remotes?
  • Can't cd to directory which has spaces in the name
  • 6 Solutions collect form web for “Is there a quick git command to see an old version of a file?”

    You can use git show:

    $ git show REVISION:path/to/file
    

    For example, to show the 4th last commit of the file src/main.c, use:

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

    Note that the path is from the root of the repository unless it starts with ./ or ../ to indicate a relative path. For more information, check out the man page for git-show.

    Doing this by date looks like this:

    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”.

    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.

    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.

    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 🙂

    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.

    Git Baby is a git and github fan, let's start git clone.