Git diff is a powerful command that allows you to see differences between commits, branches, and even different stages of the working tree. If you want to use it to see the differences between two specific commits, you can do so using the hash values of those commits.
In Git, every commit is associated with a unique hash value. This hash is a SHA-1 checksum of the commit’s contents and metadata, providing a unique identifier for each commit. Here’s how you can use the git diff command with these hash values to compare two commits.
Find the hash values for the commits you want to compare. You can find these hash values using the git log command. This command will display a list of all the commits in the repository, along with their associated hash values, author, date, and commit message.
This command will output something like:
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0 Author: Your Name <email@example.com> Date: Thu Jun 1 12:34:56 2023 -0700 Your commit message here commit a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t1 Author: Your Name <firstname.lastname@example.org> Date: Wed May 31 12:34:56 2023 -0700 Your previous commit message here
In this example, the hash values of the commits are the long strings of characters following the word “commit”.
Use the git diff command with the two hash values. Once you have the hash values of the two commits you want to compare, you can use the git diff command followed by the two hash values to see the differences between them.
git diff a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0 a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t1
Make sure to replace
a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t1 with the actual hash values of your commits.
This will display a line-by-line breakdown of the differences between the two commits. Additions will be shown in green with a plus sign (+) at the start of the line, and deletions will be shown in red with a minus sign (-).
Keep in mind that the order in which you input the hash values matters.
git diff <commit1> <commit2> will show you what changed from commit1 to commit2. If you want to see what changed from commit2 to commit1, you would use
git diff <commit2> <commit1>.