How can I get content of a file from git index?
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.
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 README100644 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.
There are Three ways of getting diffs with git
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