Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We only recently created the `testing` branch, so you probably remember what's on it. But suppose you've gotten pulled away for a week to work on a higher-priority project. Or suppose it's not even your branch, but one created by a collaborator. As we'll see later, branches can be transferred to other repositories, and you're going to need a convenient way to see what's on those branches.
-
git diff
can show you the differences between two branches. For example:git diff master testing
- Git looks up the commits that each branch currently points to, and gives us a diff between those commits.
- You aren't resricted to the branch that's currently checked out, either.
- If we want to see the differences between the
add-letters
andtesting
branches, you can run this command:git diff add-letters testing
- If we want to see the differences between the
- When you provide only one branch name, Git will compare the contents of the working directory to that branch.
- I can show the difference between my files and the
master
branch by running:git diff master
- If you have modified files in your working directory that haven't been committed yet, this version of the
git diff
command will show you those changes. - If everything is committed, then it will look the same as comparing two branches.
- I can show the difference between my files and the
We only recently created
the testing branch, so
0:01
you probably remember what's on it.
0:03
But suppose you've gotten pulled away for
0:05
a week to work on a higher
priority project.
0:07
Or suppose it's not even your branch,
but one created by a collaborator.
0:10
As we'll see later, branches can be
transferred to other repositories, and
0:15
you're going to need a convenient
way to see what's on those branches.
0:19
Without any arguments, git diff shows
you the differences between your working
0:22
directory and your latest commit.
0:26
But you can also specify particular
commits from your history and git will
0:29
ignore the working directory and show
you differences between those commits.
0:32
First, let me make sure the testing branch
is checked out, git checkout testing.
0:37
Now I'll look at my history with git log.
0:44
I'm going to note the SHA checksums from
the commit the master branch points to,
0:47
and the commit the testing
branch points to.
0:51
Now I'll run git diff and give it partial
SHA checksums for those two commits.
0:54
You should list the earlier commit first,
so
0:58
I'll give it the checksum
from the master branch first.
1:01
Git will show you changes for
all the commands between the first and
1:04
last command you specified.
1:07
In this case, all the changes
are within the test_decoder.rb file.
1:10
But if there are changes in multiple
files, it'll show all the files.
1:13
If I run git log -p,
1:18
you can see the individual changes
that went into this combined diff.
1:19
There's the commit where
we set up the unit test,
1:23
followed by a separate commit
where we add an additional test.
1:25
Being able to show diffs between specific
commits like this is occasionally useful.
1:29
It's kind of a pain to have to
type the commit SHAs though.
1:34
So let me run another command that
will give me the same result.
1:37
git diff master testing.
1:40
As we mentioned earlier, a branch is
just a pointer to a specific commit.
1:46
So git lets you use branch names
in place of the commit SHAs.
1:51
This lets us get a diff between
two branches much more easily.
1:54
Git looks up the commits that
each branch currently points to,
1:59
and gives us a diff between those commits.
2:02
You aren't restricted to the branch
that's currently checked out either.
2:05
If we want to see the differences between
the add-letters and testing branches,
2:08
you can run this command,
git diff add-letters testing.
2:12
That shows us that the testing
branch doesn't have the letter
2:18
translations from the add-letters branch,
but it does have the unit test code.
2:21
There's one variation on the git diff
command that I want to show you.
2:26
When you provide only one branch name,
2:30
git will compare the contents of
the working directory to that branch.
2:32
So I can show the differences
between my files and
2:36
the master branch by
running git diff master.
2:39
If you have modified files in your working
directory that haven't been committed yet,
2:42
this version of the git diff command
will show you those changes.
2:46
If everything is committed, then it will
look the same as comparing two branches.
2:50
The git div tool is always useful, but
2:54
it's especially useful
when it comes to branches.
2:56
Git diff can help you figure out
exactly what's on that branch,
2:59
whether it was created by you or
by a collaborator
3:02
You need to sign up for Treehouse in order to download course files.
Sign up