git diff is a command-line tool in Git that shows the differences between various states of a repository. It helps developers see what changes have been made, whether they are between working directory and the staging area, between the staging area and the last commit, or between any two commits.

- This diagram illustrates the three main areas in Git: HEAD (latest commit), Index (staging area), and Working Tree (your current files).
git diffcompares changes between the Working Tree and Index (unstaged changes).git diff HEADcompares the Working Tree to the HEAD commit (all local changes).git diff --cachedcompares the Index and HEAD (staged but uncommitted changes).- Arrows show how each diff command lets you see changes between these different stages of your repository.
Viewing Changes Between Commits
So what git diff actually shows you all the changes done in the file after doing a commit for example:- a file say at.txt is modified here after doing a commit and here we can see that there is a difference in the file after a commit.

Illustration: Changes between two Commits as shown below as follows:
So now if you want to see the changes between the two commits use the following command: git diff commit-id1 commit-id 2 here we can see that in our first Commit Hello Geeks for Geeks ? is coming and in our second commit Hello Geeks for Geeks ? is coming here.

Shows difference for Staged files
So now if we want to see the changes between the previous commit and currently staged files we can use the following command:
git diff --staged. Also, there is one more command which is git diff --cached which we can use for the same use case. Also, we can use
git status -v which is just like a synonym for --staged one command.

Using git diff for comparing branches
For seeing the changes between different branches we will use the command git diff name_of _the_branch1 name_of_the_branch2. Now if we want to see all the changes on branch_2 for that we will use the command git diff branch1_name branch2_name.

git diff branch1_name branch2_nameshows all changes present in branch2 but not in branch1; new additions in branch2 appear in green (tracked and committed in branch2).- Reversing the command (
git diff branch2_name branch1_name) highlights changes in branch1 that are missing from branch2; these appear in red. - If you add and commit a file in branch1 only, that change will show as red when comparing branch2 to branch1, meaning branch2 does not have it.
- Green color indicates changes present and committed in the compared second branch; red means changes missing from the base branch in the comparison.
- Use these diff commands to clearly identify unique changes between branches for better merging and review.
Showing Both staged and unstaged Changes
For seeing all the staged and unstaged changes in git we use the following command: git diff HEAD

We can also use one more command for achieving this particular use case git status -vv it actually tells which changes are staged for commit and which are not.

Here all the changes which are under - sign are the changes that are not staged for commit.
Showing Differences for a Specific File or Directory
git diff file_nameIt shows all the changes between the previous commit of the specified file and the locally-modified version that has not yet been staged. All the changes which are under the - sign are not staged.

It also works for directories and shows the changes between the previous commit of all files in the specified directory and the locally modified
versions of these files that have not been staged. Here all those changes which are coming in green color are the changes that are not staged.

To show the difference between some version of a file in a given commit and the local HEAD version you can specify the commit id of that commit and can compare it with the local head version you want. The local head version is basically the most recent change done in the file.

The change which is coming in the green color is the local head version that is the present state of the file which is the most recent change done in the file. Now if you want to see the version between two separate commits: git diff commit-id-1 commit-id-2 file_name.

Here we can see the version between two separate commits in green color for the respective commits ids. To show the difference between the version specified by the hash a1e7045 and the latest commit on the branch for the directory we can use this command:
git diff commit-id branch_name directory_name/
Showing differences between the current version and the last version
git diff HEAD^ HEADThis command shows the changes between the previous commit and the current commit.

Patch-compatible diff: Sometimes we just need a diff to apply using a patch. So the command for that would be:
git diff --no-prefix > some_file.patchThis will create a patch_file because of this > symbol and that patch file will contain changes of the file such as changes that are staged and which are not staged. In general, it shows us the line changes the lines in green color are the changes that are recently made. So it works exactly the same just like the git diff.

Difference between two Commit or Branch
To view the difference between the two branches we use the following command:
git diff branch1_name branch2_name
To view the difference between two commit id's following command is used:
git diff commit-id-1 commit-id-2 
To view difference with current branch
git diff name_of_branch
To view the difference with commit_id
git diff commit id
To view the summary of changes
git diff --stat branch or we can write git diff -stat commit_id

To view files that changed after a certain commit
git diff --name-only commit-id
To view files that are different than a branch
git diff --name-only branch_name
To view files in a folder that changed after a commit
git diff --name-only commit-id folder-path
