How to use the command 'git diff' (with examples)

How to use the command 'git diff' (with examples)

Git diff is a command used to show the changes made to tracked files in a Git repository. It provides a detailed view of the differences between the working directory, staging area, and the last commit. This command is extremely useful for reviewing changes before committing them and understanding the modifications made to files.

Use case 1: Show unstaged, uncommitted changes

Code:

git diff

Motivation:

Using git diff without any arguments displays the changes made to the working directory that have not been staged or committed yet. This is useful for reviewing the modifications before committing them to ensure everything is as expected and to catch any mistakes or unintended changes.

Explanation:

The command git diff compares the current working directory with the last commit. It analyzes the differences between the two and displays the changes line by line. The output includes removed lines (lines present in the last commit but not in the working directory) and added lines (lines present in the working directory but not in the last commit).

Example output:

diff --git a/file.txt b/file.txt
index 634b5b6..97e6e97 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,4 @@
-Hello, World!
+Hello, Git!
+This is a test file.

Use case 2: Show all uncommitted changes (including staged ones)

Code:

git diff HEAD

Motivation:

Using git diff HEAD provides a comprehensive overview of all the changes made, including both staged (added to the staging area) and unstaged modifications. It allows developers to review all the changes they have made since the last commit, including those that are already staged and ready to be committed.

Explanation:

The argument HEAD represents the last commit in the branch or the commit you are currently on. By specifying HEAD as an argument, the git diff command compares the changes in the working directory, staging area, and the last commit. This means that it includes both unstaged and staged changes in the output.

Example output:

diff --git a/file.txt b/file.txt
index 634b5b6..97e6e97 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,4 @@
-Hello, World!
+Hello, Git!
+This is a test file.

Use case 3: Show only staged (added, but not yet committed) changes

Code:

git diff --staged

Motivation:

Using git diff --staged is helpful when you want to review the changes that have already been staged but have not yet been committed. This allows you to verify the modifications before making a final commit.

Explanation:

The --staged option is used to compare the changes in the working directory and staging area. It shows the differences between the two, displaying the changes that are staged and ready to be committed.

Example output:

diff --git a/file.txt b/file.txt
index 634b5b6..97e6e97 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,4 @@
-Hello, World!
+Hello, Git!
+This is a test file.

Use case 4: Show changes from all commits since a given date/time

Code:

git diff 'HEAD@{3 months ago}'

Motivation:

By using git diff with a specific date expression, such as “‘HEAD@{3 months ago}’”, you can review changes within a certain time frame. This is useful when you want to see the modifications made in a project over a specific period, like the past month or week.

Explanation:

When using a date expression with git diff, Git compares the changes made in the working directory with the commits that occurred within the specified time frame. It shows the differences, allowing you to see the modifications made during that period.

Example output:

diff --git a/file.txt b/file.txt
index 634b5b6..97e6e97 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,4 @@
-Hello, World!
+Hello, Git!
+This is a test file.

Use case 5: Show only names of changed files since a given commit

Code:

git diff --name-only commit

Motivation:

The --name-only option is useful when you only want to know the names of the files that have changed since a specific commit. This can be helpful for scripting or when you want to know which files were affected by a particular commit without displaying the actual differences.

Explanation:

The --name-only option instructs Git to only display the names of the files that have changed, without showing the actual differences. By specifying the commit identifier after the option, Git compares the changes made in the working directory with the specified commit and outputs a list of the modified file names.

Example output:

file.txt

Use case 6: Output a summary of file creations, renames, and mode changes since a given commit

Code:

git diff --summary commit

Motivation:

Using git diff --summary is useful when you want a brief summary of the file creations, renames, and mode changes that have occurred since a specific commit. It provides a high-level overview of the modifications made to the repository.

Explanation:

The --summary option produces a summarized output that includes file creations, renames, and mode changes between the working directory and the specified commit. It displays the number of files changed, along with the type of modification for each file.

Example output:

create mode 100644 newfile.txt
rename oldfile.txt => renamedfile.txt (100%)
mode change 100644 => 100755 script.sh

Use case 7: Compare a single file between two branches or commits

Code:

git diff branch_1..branch_2 [--] path/to/file

Motivation:

When you want to compare the differences in a specific file between two branches or commits, using git diff branch_1..branch_2 [--] path/to/file helps. This allows you to analyze the modifications made to that specific file across the two specified references.

Explanation:

git diff branch_1..branch_2 compares the changes between two branches or commits and path/to/file specifies the path to the file you want to compare. By using this combination, Git displays the differences in that specific file between the two references.

Example output:

diff --git a/file.txt b/file.txt
index 634b5b6..97e6e97 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,4 @@
-Hello, World!
+Hello, Git!
+This is a test file.

Use case 8: Compare different files from the current branch to another branch

Code:

git diff branch:path/to/file2 path/to/file

Motivation:

When you want to compare changes between files in the current branch and another branch, using git diff branch:path/to/file2 path/to/file allows you to directly analyze the differences in those files.

Explanation:

In this case, branch:path/to/file2 refers to the file in another branch you want to compare to, whereas path/to/file specifies the file in the current branch. By executing this command, Git compares the two files and outputs the differences between them.

Example output:

diff --git a/file.txt b/file.txt
index 634b5b6..97e6e97 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,4 @@
-Hello, World!
+Hello, Git!
+This is a test file.

Conclusion:

The git diff command is an essential tool for reviewing changes and understanding the modifications made to tracked files in a Git repository. By utilizing the various use cases provided in this article, developers can inspect changes, compare files, and review modifications, ultimately improving their coding workflow and ensuring the quality of their commits.

Related Posts

How to use the command colorls (with examples)

How to use the command colorls (with examples)

The colorls command is a Ruby gem that enhances the functionality of the ls command in the terminal by adding colors and font-awesome icons to file listings.

Read More
How to use the command 'route' (with examples)

How to use the command 'route' (with examples)

The ‘route’ command in Linux is used to manipulate the IP routing table.

Read More
Using the `schroot` command (with examples)

Using the `schroot` command (with examples)

1: List available chroots schroot --list Motivation: To know what chroots are available for use.

Read More