Get startedGet started for free

Comparing versions

1. Comparing versions

Now it's time to compare versions of files at different points in time!

2. git show output

In the last video, we saw the output of git show, which contains a diff section.

3. git diff

A diff is how git shows us the difference between versions! We can perform comparisons using the git diff command. Here, we compare the last committed version of our report with a modified version that is not in the staging area by running git diff report.md.

4. Comparing to an unstaged file

The output shows two versions of the report: A is the last version that has been committed, and B is the version that we still need to add to the staging area. Generally speaking, version B will be the newest version.

5. git diff output

The line starting with two at symbols tells us what changed between the two versions. The minus 1 and 5 indicate that version A starts at line 1 and has 5 lines, and the plus 1 and 5 show that version B also starts at line 1 and has 5 lines. This makes sense when we look at the next part of the output.

6. git diff output

We finished our executive summary and deleted this task from the report in our latest commit, for version A. This is shown by the red text starting with a minus symbol, representing a line in version A that is not in version B.

7. git diff output

We added a task in version B of the report, which is not in version A. It is shown in the final line with green text starting with a plus symbol.

8. Comparing to a staged file

What if the latest version, B, is now in the staging area? We can still compare it to the last committed version using git diff, but we need to add the --staged flag to indicate we want to look at a particular revision of the file.

9. Comparing to a staged file

We get the exact same output as previously, given no further changes were made to the file now in the staging area.

10. Comparing multiple staged files

Note that if we omit the file name we'll get a comparison of all staged files versus their last committed versions.

11. Comparing two commits

We can also compare versions between commits! We have two options for this. First, we can find the commit hashes of interest using git log. Then we can run git diff followed by the two commit hashes. Git will show what changed from the first commit to the second one. So, generally, we should put the most recent commit hash second. Alternatively, Git has a shortcut for us! The word HEAD, in capitals, can be used to refer to the most recent commit! To look at older commits we can reference them to HEAD by using a tilde, then a number. So, to compare the second most recent commit with the latest commit, we replace the first hash with HEAD-tilde-one and the second hash with HEAD. Tilde-one equates to the last commit minus one, so it represents one commit prior to the latest.

12. Comparing two commits

The output shows version B, the version from the most recent commit, has one more line than version A, which is from the second most recent commit.

13. Summary

We've covered a lot! This table summarizes the commands and syntax that we discussed; please use it for reference.

14. Let's practice!

Now it's your turn to compare some files!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.