> git init . Initialized empty Git repository in /home/XXXXX/test/.git/ > ll total > touch foo > git add foo > git commit -m init [master (root-commit) cdc99b4] init 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 foo > echo "bar">foo > git log commit cdc99b4fa8cd2117015cd114bf269ab9a209e58c Date: Thu Jan 28 08:57:34 2016 +0100 init Change-Id: If5530860db17d2242e4082666042960fc423f737 > git diff --cached cdc99b4fa8cd2117015cd114bf269ab9a209e58c > cat foo bar > git status # On branch master # Changes not staged for commit: # (use "git add..." to update what will be committed) # (use "git checkout -- ..." to discard changes in working directory) # # modified: foo # no changes added to commit (use "git add" and/or "git commit -a") >
期待的结果是git diff可以输出 bar
你正常的git diff不行?
git diff不加参数就应该看到bar
git diff --cached
解释一下,git diff
对比的是 working tree 和 HEAD 之间的。而你这个应该是 git add
之后了,所以需要对比 staged 和 HEAD 之间,加 --cached
就是为了这个。
OK,之前没仔细看题目里的过程,以上回答有误,感谢 @jokester 提醒。
接着复刻了一遍题主的过程,结果如下:
结果就是 git diff
没有问题,为了保障整个过程没有疏漏所以附上的是完整截图,题主可以对照看看问题在哪儿。
另外我注意到题主在重新编辑 foo
之后没有 git add
的情况下使用的是 git diff --cached
命令,这恰恰符合我之前答案提到的(我是理解反了题目的诉求),所以不要加 --cached
就好了。附上对比: