Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

bash

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?

Example:

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

Wanted output:

[previous contents of file] foo 
like image 223
user123444555621 Avatar asked Mar 01 '11 10:03

user123444555621


People also ask

How do you see the contents of a commit in git?

Viewing a list of the latest commits. If you want to see what's happened recently in your project, you can use git log . This command will output a list of the latest commits in chronological order, with the latest commit first.

How do I view a file before a commit?

If you just want to see the diff without committing, use git diff to see unstaged changes, git diff --cached to see changes staged for commit, or git diff HEAD to see both staged and unstaged changes in your working tree. +1 Yes. git diff . htaccess does what I wanted to achieve.

How do you display a list of files added or modified in a specific commit?

OK, there are a couple of ways to show all files in a particular commit... To reduce the information and show only names of the files which committed, you simply can add --name-only or --name-status flag... These flags just show you the file names which are different from previous commits as you want...

How do you see what files were changed in a commit?

Find what file changed in a commit To find out which files changed in a given commit, use the git log --raw command. It's the fastest and simplest way to get insight into which files a commit affects.


2 Answers

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

git show :file 

See gitrevisions (which uses the term 'stage 0' for the "normal" index contents):

:[<n>:]<path>, e.g. :0:README, :README 

A colon, optionally followed by a stage number (0 to 3) and a colon, followed by a path, names a blob object in the index at the given path. A missing stage number (and the colon that follows it) names a stage 0 entry. During a merge, stage 1 is the common ancestor, stage 2 is the target branch’s version (typically the current branch), and stage 3 is the version from the branch which is being merged.

like image 148
user1686 Avatar answered Sep 24 '22 08:09

user1686


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.

like image 43
Mark Longair Avatar answered Sep 24 '22 08:09

Mark Longair