41

I have a file which has been already added to my local repository. I've modified it in the working tree, so git status shows me the file as modified. I would like to know what is the file content kept in the index before I stage the file.

I can think of two ways of doing that:

  1. revert a patch generated by git diff, and apply it on the file in the working tree
  2. use git checkout-index, point to a temporary file and read the content from there

Is there an easier way?

poke
  • 307,619
  • 61
  • 472
  • 533
zaza
  • 1,169
  • 2
  • 10
  • 21

3 Answers3

67

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

git show :file

See the gitrevisions manual for more information.

poke
  • 307,619
  • 61
  • 472
  • 533
user1686
  • 10,985
  • 2
  • 32
  • 52
8

To cat a file out of the index, I’m not sure of a preexisting scriptable way, but you can use ls-files to query the index:

$ git ls-files -s README
100644 67cfeb2016b24df1cb406c18145efd399f6a1792 0   README
$ git cat-file blob 67cfeb2016b24df1cb406c18145efd399f6a1792
# etc.

You can put the commands together like this:

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

(Although surely I am reinventing the wheel here.)

However, if you just want to open the original and your changes in an editor, use the difftool command. It copies the indexed version to a temporary file for you and opens vimdiff (or anything you want), and it is very flexible.

Josh Lee
  • 149,877
  • 34
  • 253
  • 263
4

There are Three ways of getting diffs with git

git diff graph

so to see what the difference is between the file in the working directory and the index you just need to:

git diff name_of_file

I've written about this in more detail elsewhere

Abizern
  • 129,329
  • 36
  • 198
  • 252
  • 2
    Thanks, but I'm not interested in getting a diff. I'm looking for the whole _file content_. Having the diff I can deduct what was the file content before I made the change, but I already mentioned that in the question. – zaza Feb 17 '11 at 23:50