diff
The git diff
command is used to show the differences between various Git data sources, such as working directory, staging area, and commits. It is a powerful tool for reviewing changes before committing them or for comparing different versions of a repository.
Detailed Explanation
Comparing Working Directory and Staging Area: By default,
git diff
shows the changes between the working directory and the staging area. This helps you see what changes have been made but not yet staged for commit.Comparing Staging Area and Last Commit: Using
git diff --cached
(orgit diff --staged
), you can see the differences between the staging area and the last commit. This is useful for reviewing what will be included in the next commit.Comparing Commits: You can compare two commits by specifying their commit hashes. This shows the changes between those two points in the repository's history.
Comparing Branches: You can compare the differences between two branches by specifying their names. This helps in understanding what changes are in one branch but not in another.
Comparing Files: You can also specify a file or directory to see the differences only for that specific path.
Examples
Comparing Working Directory and Staging Area:
git diffThis command shows the changes in the working directory that have not yet been staged for commit.
Comparing Staging Area and Last Commit:
git diff --cachedThis command shows the changes that have been staged for commit but not yet committed.
These examples demonstrate how git diff
can be used to review changes at different stages of the Git workflow.