search

Home  >  Q&A  >  body text

git diff uncommitted changes

> 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 <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   foo
#
no changes added to commit (use "git add" and/or "git commit -a")
>

The expected result is that git diff can output bar

为情所困为情所困2794 days ago608

reply all(3)I'll reply

  • 滿天的星座

    滿天的星座2017-05-02 09:29:13

    git diff --cached

    Explain, git diff 对比的是 working tree 和 HEAD 之间的。而你这个应该是 git add 之后了,所以需要对比 staged 和 HEAD 之间,加 --cachedThat’s what it’s for.


    OK, I didn’t read the process in the question carefully before, so the above answer is wrong. Thanks to @jokester for reminding me.

    Then I reproduced the process of asking the question, and the results are as follows:

    The result is git diff There is no problem. In order to ensure that there are no omissions in the entire process, a complete screenshot is attached. The questioner can compare it to see where the problem is.

    In addition, I noticed that the questioner used the foo 之后没有 git add 的情况下使用的是 git diff --cached 命令,这恰恰符合我之前答案提到的(我是理解反了题目的诉求),所以不要加 --cached command after re-editing foo without git add, which is exactly in line with what I mentioned in the previous answer (I am This is contrary to the question's appeal), so just don't add --cached. Attached is a comparison:

    reply
    0
  • PHPz

    PHPz2017-05-02 09:29:13

    Git diff should see bar without adding parameters

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-02 09:29:13

    Can’t you do normal git diff?

    reply
    0
  • Cancelreply