How can I see the changes between two commits?

In version control systems like Git, it’s common to want to view the differences between two commits. You can do this by using the git diff command. Here’s how:

  1. Finding the commit hash codes: First, you need to find the hash codes of the commits that you’re interested in. These hash codes are unique identifiers for each commit in your repository. You can find these hash codes by using the git log command. This command will show you a list of all the commits made in your repository, from newest to oldest. Here’s an example of how to use it:
git log

Each commit in the log is presented in this format:

commit a867b4af366350be2e7c21b8de9cc6504678a61b
Author: John Doe <jdoe@example.com>
Date:   Mon Mar 5 10:15:10 2023 -0800

    Here's an example commit message.

In this example, the commit hash code is a867b4af366350be2e7c21b8de9cc6504678a61b.

  1. Using the git diff command: Once you have the hash codes for the commits you’re interested in, you can use the git diff command to view the differences between them. Here’s how you can use it:
git diff <older-commit-hash>..<newer-commit-hash>

In the command above, replace <older-commit-hash> and <newer-commit-hash> with your actual commit hash codes. This command will show you the differences between the two commits in a line-by-line format.

For example, if you want to see the difference between commits a867b4af366350be2e7c21b8de9cc6504678a61b and b6d1b4af345350be2e6c21b7d9cd6504678a61c, you would run:

git diff a867b4af366350be2e7c21b8de9cc6504678a61b..b6d1b4af345350be2e6c21b7d9cd6504678a61c

This command will output the changes between the two commits, including added lines (marked with a “+”) and removed lines (marked with a “-“).

Remember, in the context of Git, “older” and “newer” refer to the order in which commits were made, not when the changes they represent were made. So the “older” commit is the one that was made first, and the “newer” commit is the one that was made last.

If you want to see these changes in a more user-friendly format, you can use a GUI tool like GitKraken or SourceTree, or use the git difftool command to open the changes in a diff tool of your choice.