Git Diff

In Git, the diff command is used to compare the differences between two versions of a file or between two commits. It shows you the specific changes that have been made to the file, including lines that have been added, removed, or modified.

There are several ways to use git diff, depending on what you want to compare. Here are a few examples:

  • To compare the differences between two commits, you can use the commit hashes:
$ git diff abc123 def456 

This will show the differences between the two commits with the hashes abc123 and def456.

  • To compare the differences between the current version of a file and the version in the most recent commit, you can use the HEAD reference:
$ git diff HEAD path/to/file 

This will show the differences between the current version of the file and the version in the most recent commit.

  • To compare the differences between the current version of a file and the version in the staging area, you can use the --staged option:
$ git diff --staged path/to/file 

This will show the differences between the current version of the file and the version that has been staged for the next commit.

Here are a few additional things to keep in mind when using git diff:

  • You can use the --name-only option to display a list of the files that have been modified, rather than the specific changes made to each file. This can be useful for quickly identifying which files have changed.

  • You can use the --stat option to display a summary of the changes made to each file, including the number of lines added and removed.

  • You can use the --color option to highlight the changes made to each file. This can make it easier to see the differences between the two versions.

  • If you want to see the differences between two branches, you can use the git diff command with the branch names:

$ git diff branch1 branch2 

This will show the differences between the two branches, including the changes made in each branch since they diverged.