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?

  • Trying to redirect 'git gc' output
  • Bash and Git Flow bug in common file
  • Get sha of the last commit before pull
  • How do I configure git to inherit my specific $PATH environment variable?
  • aborting git pre-commit hook when var_dump present
  • Why am I getting “: No such file or directory” when trying to execute a bash script?
  • Example:

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

    Wanted output:

    [previous contents of file]

  • Git pull fails with bad pack header error
  • Static web frontend for Git/Mercurial/Darcs
  • How to work on two major versions using the GitFlow model
  • Understanding and trying to apply git patch for fuse filesystem
  • Make git ignore the date in PDF files
  • The way to “reinit” repository
  • 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.