Checking differences in Git

Sometimes when we are done working on a file, or when we have the latest version of a file, we wonder what had changed. If we used SVN before, then we are probably familiar with:

svn diff file1
svn diff -r 1234:1235 file1

The first command compares the latest change of the working copy of the file from what’s on the repository. The second one is when comparing different versions of a file.

In Git we have something similar:

git diff file1
git diff 1234 1235 file1

This will compare your working copy to the latest one committed to the repository. And the other one is to compare the two different versions of a file.

Then, there’s also a shortcut if you just want to compare the latest committed file to the commit before that:

git diff HEAD^ file1

And sometimes we also need to compare the differences of branches (please check the articles section, we might have one about branches), we do this by:

git diff branch1 branch2 

Though this will display the differences of each files from that branch, if we want to only see the list of the files that was different from each branch, we can use this instead:

git diff --name-status branch1 branch2

Remember in Git there are parameters we can add to alter the result of a command, check it out by:

git help diff

Happy diffing.