git diff command is a powerful tool in Git that allows you to identify changes between commits, commit and working tree, etc. To see changes for a specific file, you would typically use this command as follows:
git diff -- <file>
<file> should be replaced by your file path. This command shows the changes between your working directory and the last commit.
If you want to see changes between two specific commits for a specific file, you can use:
git diff <commit1>..<commit2> -- <file>
In the above command,
<commit2> are the hashes of the commits you’re interested in, and
<file> is the path to your file. This will show you the differences in that file between the two commit points.
Let’s go through a concrete example.
Let’s say you’ve made several changes to a project and want to see what’s different in the
example.txt file. Here’s how you might use the
git diff command:
git diff -- example.txt
This will produce output showing what lines were added or removed in
example.txt, comparing the current state of the file in your working directory to the last committed state.
Each line of output will start with a
- if the line was removed, or a
+ if it was added. Lines that haven’t changed are also displayed, for context, but without a
+ at the start of the line.
To compare this
example.txt file across two specific commits, you could use a command like this:
git diff 1a2b3c4d..5e6f7g8h -- example.txt
5e6f7g8h would be replaced by the actual hashes of the commits you’re interested in. The output will show the changes to
example.txt between these two points in the project’s history.
git diff in this way, you can inspect changes to a specific file either in your working directory or between commits. This can be very useful when you’re trying to track down when a particular change was made, or to understand what changes have been made to a file over time.