Getting Git

A comprehensive video course from git init to Git Master.

Master: git diff

In this video we'll learn some of the additional options and uses for git diff.

First, let's take a look at the -w option. This option allows us to ignore whitespace changes.

This helps limit the noise when using git diff.

Let's demonstrate this by making some whitespace changes to File 2.

Running git diff -w outputs no changes since all my changes were whitespace.

Beyond just viewing current changes git diff can be used to compare changes over time.

For example, I can view the changes in the repository from the current state to a previous commit.

Let's demonstrate this by running git log --oneline and copying the commit SHA of a previous commit.

For now, we'll simply pass this SHA to git diff.

This outputs all the changes between the current state and the previous commit.

In this case, we add a timestamp as well as whitespace.

Sometimes, we may want to see the changes between two previous points in time.

To do so, we can pass a commit range to git diff.

We do so by passing the recent commit SHA then .. and the older commit SHA.

In this case, from our last commit of adding a timestamp to when we first added File 2.

This outputs the changes between these two commits.

There's two important points here.

First, it ignores our recent whitespace changes since we provided a commit range.

Second, the output will depend on the order. So git diff displays the changes to go from the first commit in the range to two second commit.

In this case, the difference would be to remove the timestamp line from File 2.

This can be counter intuitive. So, when you want to see what changed between two previous commits, you can pass the older commit first and the recent commit second.

In this case, git diff now shows we added the timestamp between these two commits, which reflects the changes more naturally.