2

similar to this question but how do I see what has changed between my last commit and my current state, which would have some changes.

So based on the linked question, it would be something like this

between 2 different SHAs:

git diff --name-only SHA1 SHA2

to see the differences between the tenth latest commit and the fifth latest (or so):

git diff --name-only HEAD~10 HEAD~5

Now my attempt between the current state and the last commit:

git diff HEAD HEAD~1

Note I would leave --name-only out as I am interested in seeing what was changed in the file.

Community
  • 1
  • 1
HattrickNZ
  • 3,493
  • 12
  • 40
  • 82
  • 1
    By "current state", do you mean the files you have in your work-tree? If so, have you `git add`-ed them, or not? Or do you not mean that at all; do you mean instead your current commit, i.e., `HEAD`? The key to the correct question in Git is realizing that there are three entities of concern here: the current *commit*, the *index*, and the *work-tree*. – torek Sep 12 '16 at 23:26
  • 1
    good question.current state = did a commit, changed something in a file and did not do a git add. that is what I am talking about. would be good to know how to refer to them all (e.g. HEAD) – HattrickNZ Sep 12 '16 at 23:38

3 Answers3

3

... current state = did a commit, changed something in a file and did not do a git add.

OK, in this case you want to compare the work-tree files, against something already committed. The "something already committed" is either HEAD (the current commit) or HEAD^1 aka HEAD~1, the current commit's first-parent.

While there are many git diff variants (git diff-index, git diff-files, and git diff-tree), the most basic and simple one is git diff itself, and that's the one you need here.

By default, git diff <commit-specifier> compares the specified commit to the current work-tree. So if you want to see what you have not yet git add-ed:

git diff HEAD

This gives you a normal, every-day diff that shows how to change "contents of HEAD" into "contents currently in work-tree". (You could add --name-only or --name-status to get file names only, or file names and status codes, but you said you wanted the full diffs.)

To see how to change from HEAD~1 to the current work-tree, use git diff HEAD~1.

would be good to know how to refer to them all ...

The basic git diff command can in fact do most of them (some of these capabilities were newly added in Git versions 1.6 and 1.7, but the most ancient version of Git that people use in practice today seems to be 1.7.1 or so, by which these should be available):

  • git diff <commit>: compare <commit> to the work-tree. When <commit> is the literal word HEAD you end up comparing the current commit (not the index) vs the work-tree.

  • git diff (with no extra flag arguments): compare the current index vs the work-tree. This is similar to the first, but not the same. In particular, once you git add a file, you update the version in the index, so now the index version will match the work-tree version and hence you won't see it in git diff output, even if it's not actually committed yet.

    (Of course, you can safely add some flag arguments, like --name-only. The key is that you can't add --staged or --cached here.)

  • git diff --staged (can also be spelled git diff --cached): compare HEAD (the current commit) vs the index. That is, anything you see in the previous (no-flags) git diff, you can git add to the index to update the index; then you won't see it in git diff but you will see it in git diff --staged.

  • git diff <commit1> <commit2>: compare the two commits. For instance, git diff HEAD~1 HEAD compares the one-step-back commit (HEAD~1) to the current commit (HEAD).

There are still more options; see the git diff documentation for a more complete list.

(There is a significant difference between HEAD^number and HEAD~number, but only when the number part is not 1. The tilde-suffix syntax moves back some number of first parents while the hat-suffix syntax selects the n'th parent, which really only means anything for merge commits. You can omit the number and repeat the hat suffix, e.g., HEAD^^^^, and it means the same thing as HEAD~4. When you're just going back one, use whichever you find easier to type: the number defaults to 1.)

Community
  • 1
  • 1
torek
  • 330,127
  • 43
  • 437
  • 552
1
git diff HEAD^ HEAD

Previous commit can be accessed by using HEAD^, so this command would make you see the difference in your current commit and previous commit.

You could use git diff @~..@ from 1.8.5 version.

Rishi
  • 1,113
  • 7
  • 12
0

git diff-index <commit> compares <commit>'s tree to the work tree, the files on the filesystem, and plus --cached to compare <commit>'s tree to the index. If you want to see the diff in files, plus -p.

ElpieKay
  • 19,185
  • 5
  • 22
  • 36