How to view file diff in git before commit
Asked Answered
R

11

589

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.

Redmer answered 6/4, 2012 at 6:3 Comment(0)
P
953

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
Pillow answered 6/4, 2012 at 6:4 Comment(5)
check out git add -p. Review every change, selectively approve changes to stage, abort at any time if you change your mind, and even inline edit a chunk. I never git add without it.Halfcock
how can you exit the file?Unveiling
@Kick Try pressing qCommunicative
also, if you want to remove the silly 'a/' and 'b/' prefixes in diff result, you can set git config --global diff.noprefix true.Diabetic
great, this also works without file parameter. I use this when I checked out another file in my current branch. thanksAboveboard
C
79
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.

Colous answered 6/4, 2012 at 9:13 Comment(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.Turtleneck
B
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
Bartz answered 9/3, 2015 at 22:18 Comment(0)
A
22

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.

Abject answered 12/12, 2016 at 17:3 Comment(0)
R
16

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

Rom answered 6/4, 2012 at 6:7 Comment(2)
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.Parnell
Didn't work for me. "Diff Editor" simply was not shown and committed without asking.... TortoiseGit is much better.Consanguineous
R
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.

Rebato answered 6/4, 2012 at 6:59 Comment(5)
I agree - it also allows you to edit the commit message while looking at the diff.Downe
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.Redmer
Yeah if you stick to the GUI there are a lot of cool features like git bisect that aren't really accessible.Ethanethane
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.Philipines
There are also text user interfaces, that run in terminal. One worth checking out is tig "text-mode interface for Git".Sumach
M
15

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

Mode answered 6/6, 2017 at 12:20 Comment(1)
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.Heroworship
P
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.

Philipines answered 29/6, 2015 at 8:46 Comment(0)
H
4
git difftool -d HEAD filename.txt

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

Heterosexuality answered 27/2, 2018 at 10:12 Comment(0)
R
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.

Retractile answered 5/3, 2021 at 15:10 Comment(0)
T
0

BEFORE COMMIT

to be sure what is about to be committed:

git diff --cached

Any changes including not committing (not staged)

git diff
  • use arrow key to scroll down.
  • press Q to exit diff
Turku answered 4/1 at 20:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.