How can I get content of a file from git index?

。_饼干妹妹 提交于 2019-11-30 01:27:18
grawity

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 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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!