How to view file diff in git before commit

GitGit Diff

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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.

Solution 3 - Git

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

Solution 4 - Git

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.

Solution 5 - Git

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

Solution 6 - Git

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.

Solution 7 - Git

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

Solution 8 - Git

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.

Solution 9 - Git

git difftool -d HEAD filename.txt

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

Solution 10 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSauce McBossView Question on Stackoverflow
Solution 1 - GitAmberView Answer on Stackoverflow
Solution 2 - GitouahView Answer on Stackoverflow
Solution 3 - GitkenorbView Answer on Stackoverflow
Solution 4 - GitanisbetView Answer on Stackoverflow
Solution 5 - GitvhallacView Answer on Stackoverflow
Solution 6 - GitlprsdView Answer on Stackoverflow
Solution 7 - GitDisplay NameView Answer on Stackoverflow
Solution 8 - GitVitalyBView Answer on Stackoverflow
Solution 9 - GitTomachiView Answer on Stackoverflow
Solution 10 - GitbaptxView Answer on Stackoverflow