Checking out a file from a specific commit in Git

To check a single file from a specific Git commit, just specify the commit and file path in git checkout command.

Command syntax

$ git checkout <commit> -- <file-path>

Examples

# Checkout asset/css/style.css from commit 8bc723c
$ git checkout 8bc723c -- asset/css/style.css

# Checkout asset/css/style.css from the parent commit.
$ git checkout HEAD~ -- asset/css/style.css

Undoing modifications of a file in Git

To discard the changes of a file no matter it is staged or not:

# Undo modifications of a file from working tree and index
$ git checkout -- <file>

# Examples:
# Undo modifications of index.php
$ git checkout -- index.php

If fact, git status tells you how to undo the changes:

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   index.php

Note: stash command is a safer way to discard changes, because it allows you to revert the changes again. See stashing changes for how to use it.