You're working away, you get distracted, you come back to your computer,
you forget where you were... the thing you need is Diff.
Git Hub & Git Foundations
Diff
Welcome to another episode of Git Hub Foundations.
I'm Tim Berglund and today we're going to look at the Diff command.
We're gonna take a look at three different ways to use it.
Diff git diff
If you make one simple change and then commit it right away,
you're not gonna need Git to tell you what you've done.
You probably keep track of that in your head.
But there are lots of times when you need to examine
how content has changed. There are three main ways
to do this, all with one command: Diff.
One of the great things about looking at Diff
is it helps solidify what we learned about the staging area
when we were learning how to commit.
Now, suppose you change a file and then get called away
from you computer for a little bit and then come back,
how are you gonna remember what you were doing?
To put that a different way, you wanna know what changes
you've made to your files that you haven't staged yet.
This is also handy if you use development tools that automatically
make changes to your code for you and you wanna find out what
those changes are. Just type git diff and you'll get a description of exactly
how your working tree, that is, just your files,
differs from your staging area.
[Diff --staged, HEAD]
Now, suppose you'd already staged those files and you wanna know
how the things in your staging area are different from the most recent commit
in history. Just type git diff--staged, and you'll see those changes.
As a nice little bonus, going through this exercise
can really help solidify your understanding of how the staging area works.
Just because you staged a file doesn't mean you can't change it again.
If you go on making changes to that same file, the status command
will tell you that the file is staged, and has unstaged changes
at the same time. If you've done this, you might want the diff command
to skip over the staging area, and tell you about all the changes you've made
to the file since your last commit. Typing git diff HEAD
compares your working tree to the head commit
which is just another name for the most recent commit
in history. And once you start learning how to do things
like branch and push and pull, this last pattern is gonna come in
really handy. You can use it to compare two branches to one another,
or maybe two prior commits in history
to get an idea of what changes took place in the past.
[Diff --color-words, --word-diff]
Now you might notice that that last Diff
is reported in a way that's a little verbose.
I only changed one word, but Git is telling me
I changed an entire line. This is literally how Git processes
text file differences, but it's not necessarily helpful to look at.
You can add the --color-words or --word-diff switch
to any Diff command to get it to give you
an easier to read report of small changes to long lines.
This can be a real life saver sometimes.
Another handy trick is to get Diff to suppress all that patch output
and just tell you about the files that have changed.
Use the --stat switch for this. This is great when you've got
a complex diff but you might just need to zero in
on one particular file that's changed.
Tracking changes is what Git does. The Diff command is your window
into the work you've done and how Git sees the changes.
It's a great command to know.
[Thanks for watching]
Thanks for watching this episode of Git Hub Foundations
on the Diff command. Subscribe to Git Hub Guides here,
and as always, please leave us a question or a comment,
anything like that down below, and if you wanna see more training videos,
check out these guys you see down here.
Thanks!
♪ (jazz) ♪