Git: Show content of file as it will look like after committing

After reading Git pre-commit hook : changed/added files, the following question arose:

Given I have a file with both staged and unstaged changes, how can I display a preview of the file’s contents after staging?

  • How can I set up autocompletion for Git commands?
  • Git bash won't let me pull
  • In Git Bash on Windows 7, Colors display as code when running Cucumber or rspec
  • Git bash win32exception: Failed to write credentials
  • Implement .gitignore behavior in a shell script?
  • When I have git log print out as oneline, how do I reverse it?
  • Example:

    echo "foo" >> file
    git add file
    echo "bar" >> file

    Wanted output:

    [previous contents of file]

  • How can a shell script get input from an external editor (like Git does)?
  • How do I undo a `git reset --hard HEAD~1`?
  • To invoke Notepad++ from Git bash
  • Put a Rails app in public git, keep private details private
  • Git: Update local branch from master without changing locally changed files
  • Gemfile syntax error: <<<<<<< HEAD when trying to start localhost
  • 4 Solutions collect form web for “Git: Show content of file as it will look like after committing”

    Use the : prefix to access objects in the current index (staged but not yet commited).

    git show :file

    See gitrevisions (emphasis mine):

    <rev>:<path>, e.g. HEAD:README, :README, master:./README

    A suffix : followed by a path names the blob or tree at the given path in the tree-ish object named by the part before the colon. :path (with an empty part before the colon) is a special case of the syntax described next: content recorded in the index at the given path.

    Update: the answer from grawity has a much neater solution

    This recipe is from jleedev’s answer to another question:

    git cat-file blob $(git ls-files -s file | awk '{print $2}')

    You might want to create a git alias for that if you’re going to use it often.

    You can do git diff –cached, but this isn’t exactly what you want.

    git grep -h --cached ^ -- file

    works for me.

    Just have a look at the top answer for this question: How do I show the changes which have been staged?

    The --cached option is what you want.

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