Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view file diff in git before commit

Tags:

git

git-diff

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.

like image 987
Sauce McBoss Avatar asked Apr 06 '12 06:04

Sauce McBoss


People also ask

How do I see diff before commit?

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.

How do I see files changed in a specific commit?

To find out which files changed in a given commit, use the git log --raw command. It's the fastest and simplest way to get insight into which files a commit affects.


2 Answers

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 
like image 96
Amber Avatar answered Sep 20 '22 17:09

Amber


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.

like image 43
ouah Avatar answered Sep 19 '22 17:09

ouah