如何从 git index 获取文件内容?
我有一个文件已添加到我的本地存储库中。我已经在工作树中修改了它,因此 git status 向我显示了修改后的文件。我想知道在暂存文件之前索引中保存的文件内容是什么。
我可以想到两种方法:
- 恢复
git diff
,并将其应用到工作树中的文件上 - ,使用
git checkout-index
,指向一个临时文件并从那里读取内容
有没有更简单的方法?
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:
- revert a patch generated by
git diff
, and apply it on the file in the working tree - use
git checkout-index
, point to a temporary file and read the content from there
Is there an easier way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
:
前缀访问当前索引中的对象(已暂存但尚未提交)。有关详细信息,请参阅 gitrevisions 手册。
Use the
:
prefix to access objects in the current index (staged but not yet commited).See the gitrevisions manual for more information.
要从索引中删除文件,我不确定是否存在预先存在的可编写脚本的方式,但您可以使用 ls-files 来查询索引:
您可以将命令放在一起,如下所示:(
尽管当然,我在这里重新发明了轮子。)
但是,如果您只想在编辑器中打开原始文件和所做的更改,请使用
difftool
命令。它将索引版本复制到临时文件中并打开 vimdiff (或任何你想要的),它非常灵活。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:You can put the commands together like this:
(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 opensvimdiff
(or anything you want), and it is very flexible.有三种方法可以使用 git
来查看工作目录中的文件之间有什么区别以及您只需要的索引:
我已经更详细地写过此内容 其他地方
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:
I've written about this in more detail elsewhere