Comparing Specific Files with Git Diff
While the previous examples showed how to compare changes across the entire codebase, Git Diff also allows you to compare specific files or directories. This can be particularly useful when you want to focus on a specific part of your project.
Comparing a Single File
To compare the changes in a specific file between two commits, you can use the following command:
git diff <commit1> <commit2> -- <file>
Replace <commit1>
and <commit2>
with the commit references you want to compare, and <file>
with the path to the file you want to compare.
For example, to compare the changes in the main.cpp
file between the develop
and master
branches, you would run:
git diff develop master -- main.cpp
Comparing Multiple Files
You can also compare changes in multiple files by specifying their paths:
git diff <commit1> <commit2> -- <file1> <file2> <file3>
Comparing Directories
To compare the changes in an entire directory between two commits, you can use the following command:
git diff <commit1> <commit2> -- <directory>
Replace <directory>
with the path to the directory you want to compare.
Excluding Files from the Comparison
If you want to exclude certain files or directories from the comparison, you can use the --
separator to indicate the end of the commit references and the beginning of the file paths. For example:
git diff <commit1> <commit2> -- :<pathspec>
Here, <pathspec>
is a file path pattern that specifies the files or directories you want to exclude from the comparison.
By mastering the techniques for comparing specific files and directories, you can quickly identify and understand the changes relevant to your work, making the Git Diff command a powerful tool in your development workflow.