Git provides a command
git diff which is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files, and more. This command is utilized to make a diff, which is a readable report of the changes between two data sets.
Specifically, if you want to compare your current working directory (uncommitted changes) with the last commit, you can use the following command:
git diff HEAD
Here’s a breakdown of what this command does:
git diff: This is the base diff command in Git that calculates and displays the differences.
HEAD: This refers to the last commit in the currently-checked-out branch.
The command will output the differences between the current uncommitted changes in your working directory and the last commit. It will show what was added or removed to each file since the last commit.
The output of
git diff will be in diff format, where additions to the file are prefixed with a ‘+’ and removals from the file are prefixed with a ‘-‘. Each changed section is called a “hunk”, and the diff output will show you the file name, the function or area of the file where the change happened (for some file types), and the changes in that hunk.
If you only want to see a summary of changes (i.e., which files have changed and how many lines were added or removed), you can use the
--stat flag, like this:
git diff --stat HEAD
This command will only show a high-level summary of changes, and won’t show the content of what was added or removed.
Understanding the differences between your working directory and the last commit can be invaluable in coding and debugging processes. Using
git diff effectively allows you to identify and isolate changes in your codebase, ensuring your commits are logical, minimal, and error-free.