Correct usage of git diff

06-09-2023

As a programmer, Git is almost an indispensable tool for us. As a version control system, Git helps us manage file modification and version traceability, especially when developing large-scale software projects, Git can help manage code merging among team members, avoiding problems such as conflicts and code loss.

In Git, the diff command is one of the important tools to help us compare the differences between files and versions, and is often used by programmers. However, although the diff command seems simple, there are still many details to pay attention to in the specific usage. This article will introduce the correct usage of the git diff command in detail.

  1. Basic syntax

The basic syntax of Git diff is as follows:

git diff [] [] [ --] [ ...]

Among them, options is optional, commit is one of the two versions to be compared, the default is HEAD, and path is the specified path, which is optional.

Note: '--' is a separator, and the following path indicates a file or folder.

For example, compare the difference between the workspace and the current version:

git diff

Compare the difference between the workspace and the current version of a file :

git diff file1.txt

Compare the differences between the two specified commits:

git diff abc123 def456

  1. Detailed Parameters

There are many optional parameters for the git diff command, the following are some of the more important ones:

  • - -cached: Compare the difference between the staging area and the latest version of the current branch. For example:

git diff --cached

  • --staged: Same as the --cached option. is another use of the above options.

git diff --staged

  • --shortstat: output short statistics, including the number of files modified, the number of lines inserted and deleted row count etc. For example:

git diff --shortstat

Output:

2 files changed, 10 insertions(+), 5 deletions(-)< /p>

  • -w or --ignore-all-space: Ignore differences between spaces and tab keys.If the -w option is used alone, the differences between the files stored in the current workspace and the current branch will be compared; if used with other parameters, the differences between the specified versions will be compared. For example:

# Compare the difference between the workspace and the current version, ignoring the changes of spaces and tab keys git diff -w# Compare the difference between two submissions, ignoring spaces and tab keys Changes git diff -w abc123 def456

  • --name-only: Only display the file name of the changed file, not the specific difference content. For example:

git diff --name-only

Output:

file1.txtfile2.txt

  • --name-status: Display the filename and change type (i.e. modified, added, deleted, etc.) of changed files. For example:

git diff --name-status

Output:

M file1.txtA file3.txtD file4.txt

  • -U/--unified=: Display the number of lines of diff context. Defaults to 3 if not specified. For example:

# Display 5 lines of difference context content of changed files git diff -U5# Compare the difference between two commits, display 10 lines of difference context git diff -U10 abc123 def456

  1. Other usages

In addition to the usages introduced above, Git diff has other usages, such as:

  • Comparing the differences between two branches:

git diff branch1..branch2

  • Comparing the differences between two labels :

git diff tag1..tag2

  • Compare the difference between a commit and the current branch:

git diff commit-id

  • Output in blog format:

git diff --no-prefix > myfile.patch

Use the above command to output the output of diff in the format common to blogs.

  • Compare different branches or different warehouses:

git diff origin/master..HEAD

Among them, origin/master is the remote branch. This command can compare remote branches and local branches at the same time, or compare the differences between two local branches.

  1. Summary

Git diff command is one of the very important tools in Git version control, this article introduces the basic syntax of git diff And the parameters are explained in detail, and some special usages are given.

When using Git diff, pay attention to actual needs, choose appropriate parameters, and avoid unnecessary impact. Also, be careful when comparing different branches or different repositories.

Finally, the correct use of Git diff in the development process can help developers better manage code, better organize the development process, and improve project development efficiency.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us