The git diff
command is used to show the differences between different states of a Git repository. It allows you to compare changes between commits, branches, or the working directory. The basic syntax of git diff
is as follows:
git diff <commit> <commit>
Here are a few common use cases of git diff
:
-
Compare Working Directory with the Last Commit: To see the changes made in your working directory compared to the last commit, you can run the following command:
git diff
This command shows the differences between the current state of your files in the working directory and the last commit.
-
Compare Staged Changes with the Last Commit: To see the changes that are staged (added to the index) compared to the last commit, you can use the following command:
git diff --staged
This command shows the differences between the staged changes and the last commit.
-
Compare Two Commits: To compare the changes between two specific commits, you can provide their commit hashes or references to the
git diff
command. For example:git diff commit1 commit2
Replace
commit1
andcommit2
with the commit hashes or references you want to compare. This command shows the differences between the two specified commits. -
Compare Branches: To compare the changes between two branches, you can provide their branch names or references to the
git diff
command. For example:git diff branch1 branch2
Replace
branch1
andbranch2
with the names or references of the branches you want to compare. This command shows the differences between the two specified branches.
These are just a few examples of how to use git diff
. The git diff
command provides many options and flags to customize the output and compare different states of your Git repository. You can refer to the Git documentation or use git diff --help
for more detailed information and usage examples specific to your needs.