How do you show the differences between two specific commits?
Posted by DavidLee
Last Updated: June 19, 2024
To show the differences between two specific commits in Git, you can use the git diff command followed by the commit hashes (or references) you want to compare. Here’s how you can do it:
Syntax:
git diff <commit1> <commit2>
Example:
If you have two commits with hashes abc1234 and def5678, you would run the following command:
git diff abc1234 def5678
Additional Options:
1. Show changes for a specific file: If you're interested in the changes that occurred in a specific file between two commits, you can include the file path at the end:
git diff abc1234 def5678 -- path/to/file
2. Show changes in a more visual format: You might want to use --color-words for a more word-oriented diff:
git diff --color-words abc1234 def5678
3. Use git show for a single commit: If you want to see the changes made in a single commit, you can use git show:
git show abc1234
4. Comparing branches: If you're comparing the differences between two branches, replace commit hashes with branch names:
git diff branch1..branch2
Viewing Commit History:
To find out the commit hashes, you can use:
git log
This will display a list of commits in your repository, including the commit hashes, author information, and commit messages.
Conclusion:
Using git diff allows you to inspect the exact changes between two commits, which can be invaluable for code review or debugging purposes.