576

This often happens to me:

I'm working on a couple related changes at the same time over the course of a day or two, and when it's time to commit, I end up forgetting what changed in a specific file. (This is just a personal git repo, so I'm ok with having more than one update in a commit.)

Is there any way to preview the changes between my local file, which is about to be checked in, and the last commit for that file?

Something like:

git diff --changed /myfile.txt

And it would print out something like:

line 23
  (last commit): var = 2+2
  (current):     var = myfunction() + 2

line 149
  (last commit): return var
  (current):     return var / 7

This way, I could quickly see what I had done in that file since it was last checked in.

Franta
  • 986
  • 10
  • 17
Sauce McBoss
  • 6,567
  • 3
  • 20
  • 22

10 Answers10

938

If you want to see what you haven't git added yet:

git diff myfile.txt

or if you want to see already added changes

git diff --cached myfile.txt
Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29
Amber
  • 507,862
  • 82
  • 626
  • 550
78
git diff HEAD file

will show you changes you added to your worktree from the last commit. All the changes (staged or not staged) will be shown.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • 1
    Your solution works. But I am a bit confused. HEAD points to the latest commit number. So, when we git add, the index of working directory is updated and not the HEAD. So how does it shows the difference. – Mav55 Jan 12 '18 at 16:11
22

To check for local differences:

git diff myfile.txt

or you can use a diff tool (in case you'd like to revert some changes):

git difftool myfile.txt

To use git difftool more efficiently, install and use your favourite GUI tool such as Meld, DiffMerge or OpenDiff.

Note: You can also use . (instead of filename) to see current dir changes.

In order to check changes per each line, use: git blame which will display which line was commited in which commit.


To view the actual file before the commit (where master is your branch), run:

git show master:path/my_file
kenorb
  • 155,785
  • 88
  • 678
  • 743
21

Another technique to consider if you want to compare a file to the last commit which is more pedantic:

git diff master myfile.txt

The advantage with this technique is you can also compare to the penultimate commit with:

git diff master^ myfile.txt

and the one before that:

git diff master^^ myfile.txt

Also you can substitute '~' for the caret '^' character and 'you branch name' for 'master' if you are not on the master branch.

anisbet
  • 2,662
  • 2
  • 20
  • 12
16

Did you try -v (or --verbose) option for git commit? It adds the diff of the commit in the message editor.

vhallac
  • 13,301
  • 3
  • 25
  • 36
  • Good answer. This can provide information in commit editor , makes me commit more easily. Is there any way to close the information area of `Changes not staged for commit:` which can make commit editor more clean. – Radian Jheng Oct 16 '16 at 06:55
  • Didn't work for me. "Diff Editor" simply was not shown and committed without asking.... TortoiseGit is much better. – hfrmobile Jul 31 '23 at 15:30
15

I think this is the perfect use case warranting a GUI. - Although I totally understand that it can also be achieved well enough within the command line.

Personally, every commit of mine, I do from the git-gui. In which I can make multiple atomic commits with separate hunks/lines if it makes sense to do so.

Gut Gui enables viewing of the diffs in a well formatted colored interface, is rather light. Looks like this is something you should checkout too.

lprsd
  • 84,407
  • 47
  • 135
  • 168
  • I agree - it also allows you to edit the commit message while looking at the diff. – François Apr 06 '12 at 10:47
  • Good to know, but I'd like to stick to CLI. I've used a couple packages that had it, but I've moved to strictly terminal / vim for my workflow. Thanks though. – Sauce McBoss Apr 07 '12 at 01:20
  • Yeah if you stick to the GUI there are a lot of cool features like `git bisect` that aren't really accessible. – NoBugs Mar 15 '14 at 04:22
  • 2
    I agree - There are many options for which it is much faster and makes more sense to use command line. However, previewing changes isn't one of them. – VitalyB Jun 29 '15 at 08:46
  • There are also text user interfaces, that run in terminal. One worth checking out is `tig` "text-mode interface for Git". – smido Oct 17 '19 at 08:12
15

On macOS, go to the git root directory and enter git diff *

Display Name
  • 4,502
  • 2
  • 47
  • 63
  • easily my favorite answer!(and works on linux as well) I was wanting a way to do this so that I can make a good sumary for my git commit comment incase I forgot some of the things I did. – Jieiku Jan 06 '22 at 09:04
6

The best way I found, aside of using a dedicated commit GUI, is to use git difftool -d - This opens your diff tool in directory comparison mode, comparing HEAD with current dirty folder.

VitalyB
  • 12,397
  • 9
  • 72
  • 94
4
git difftool -d HEAD filename.txt

This shows a comparison using VI slit window in the terminal.

Tomachi
  • 1,665
  • 1
  • 13
  • 15
0

The diff can be done with git diff (followed by the filename or nothing if you want to see the diff of all modified files). But if you already did something like git add *, you have to undo with git restore --staged . first.

baptx
  • 3,428
  • 6
  • 33
  • 42