Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

This often happens to me:

I'm working on a couple of 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

1 Answer

0 votes
by (62.9k points)

Another technique which allows you to compare a file to the last commit which is more pedantic:

git diff master myfile.txt

The lucrativeness of this technique is that 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.

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...