git diff

Git diff (Git Diff:)

Description

Description

The git diff is a multi-function Git command, which is used to compare changes committed in Git. Particularly, with the help of this command, you can take two input data sets and output the modifications between them. While executing, this command runs a diff function on Git data source. Commonly, it is used in combination with git status and git log commands for analyzing the state of a git repository.

Diff outputs

Diff outputs (差异输出)

Diffing can have multiple outputs that will be discussed below. (差异可以有多个输出,将在下面讨论。)

Raw output format

Take a look at the commands below to create a simple repository:

mkdir test_repo
cd test_repo
touch test.txt
echo "this is a git diff test example" > test.txt
git init .
#Initialized empty Git repository in /Users/kev/code/test/.git/
git add test.txt
git commit -am "add diff test file"
#[master (root-commit) 9e2dcac] add diff test file
#1 file changed, 1 insertion(+)
#create mode 100644 test.txt

If you want the git diff to have an output, you have to change the content of the test.txt file after adding it to the created repository. You can do it by executing the following command:

echo "this is a diff example" > test.txt

Only now we can view a diff and discuss output. Executing git diff will produce the following:

diff --git a/test.txt b/test.txt
index 6b0c6cf..b37e70a 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-this is a git diff test example
+this is a diff example

Diff input sources

Below you can find the comparison input of diff. As a result, a/test.txt and b/test.txt will be passed to the diff. (您可以在下面找到diff的比较输入。因此, a/test.txt和b/test.txt将被传递到diff。)

diff --git a/test.txt b/test.txt

Meta data

This line shows some internal Git meta data .The numbers in this output match to Git object version hash identifiers. (此行显示一些内部Git元数据。此输出中的数字与Git对象版本哈希标识符匹配。)

index 6b0c6cf..b37e70a 100644

Symbols for changes

These lines show the symbols assigned to each diff input source. Changes from a/test.txt are marked with a — and the changes from b/test.txt are marked with the +++ symbol. (这些行显示分配给每个差异输入源的符号。来自a/test.txt的更改标记为— ,来自b/test.txt的更改标记为+ + +符号。)

--- a/test.txt
+++ b/test.txt

Diff chunks

A diff doesn’t show the whole file. It shows only modified lines. That modified portion is called a “chunk”. The advantage of the chunks is that they show lines before and after the changes, so you can have a better understanding of the modifications. (差异不会显示整个文件。它只显示修改过的线条。该修改部分称为“块”。区块的优势在于它们在更改之前和之后显示线条,因此您可以更好地了解修改。)

@@ -1 +1 @@
-this is a git diff test example
+this is a diff example

The first line is the chunk header. Each chunk is placed by a header within @@ symbols. The changes made to the file are summarized in the header. (第一行是区块标头。每个区块由@ @符号内的标头放置。对文件所做的更改汇总在标题中。)

Highlighting changes

Highlighting changes (突出显示更改)

You can use the following 2 tools for highlighting changes so that they will be more visible. (您可以使用以下2种工具来突出显示更改,以便更明显地显示更改。)

git diff –color-words

The first way of highlighting changes is the special mode that git diffproposes: ‐‐color-words. It tokenizes added and removed lines by whitespace and then diffs those.

git diff --color-words
diff --git a/test.txt b/test.txt
index 6b0c6cf..b37e70a 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
this is agit difftest example

git diff-highlight

In the case of cloning the git source, a sub-directory call contrib will be found. This sub-directory contains tools that are related to Git. One of these tools is called diff-highlight. It highlights changed sub-word pieces.

git diff | /your/local/path/to/git-core/contrib/diff-highlight/diff-highlight
diff --git a/test.txt b/test.txt
index 6b0c6cf..b37e70a 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-this is a git diff test example
+this is a diff example

Diffing binary files

Diffing binary files (差异二进制文件)

Git diff can be run not only on text files, but also on binary ones. The default option can sometimes be not very useful. (Git diff不仅可以在文本文件上运行,还可以在二进制文件上运行。默认选项有时可能不是很有用。)

git diff
Binary files a/script.pdf and b/script.pdf differ

Git has a feature allowing you to specify a shell command to convert the binary files content into text prior to performing the diff. However, a little set up may be required. First of all, you should define a textconv filter with the description of the conversion way of a certain type of binary to a text. For example, you can use a simple utility called pdftohtml (available through homebrew) to convert PDF into HTML. There are two ways of setting it up: by editing the .git/config file, or globally, by editing .gitconfig.

[diff "pdfconv"]
textconv=pdftohtml -stdout

Then you need to connect one or more file patterns with the pdfconv filter, which can be done by creating a .gitattributes file in the root of the repository.

*.pdf diff=pdfconv

After the configuration, git diff will firstly run the binary file via the configured converter script and diff the output of the converter. Using the same technique, you can also have helpful diffs from all types of a binary file (zips, jars, and other archives).

Comparison of files: git diff file

Comparison of files: git diff file

The git diff command has also an explicit file path option. The Git diff operation will go through the specific file once the file path passed to git diff. In the example below, the ./path/to/file argument will compare the modifications in the working directory against the index and display the changes that haven’t been staged yet. It will perform the comparison against HEAD.

git diff HEAD ./path/to/file

Comparison of all changes

Comparison of all changes (所有变更的比较)

In order to compare changes throughout the whole repo, you should run git diff without a file path. So, you can invoke the examples above without the ./path/to/file argument and have the same results across all files in the local repository.

Changes since the recent commit

Changes since the recent commit (自最近提交以来的更改)

Initially, git diff shows all of the uncommitted changes since the most recent commit:

git diff

Comparison of files between two commits

Comparison of files between two commits (两个提交之间的文件比较)

Thegit diff command can be passed to Git refs, such as names of head, tags, and branches. Every commit in Git has its commit ID which can get when executing git log. The commit ID can also be passed to git diff.

Comparison of branches

Comparison of branches (分支机构对比)

The comparison of branches is executed similarly to other ref inputs to git diff. Let’s see dot operator examples:

git diff branch1..branch2

The two dots in the example above show that the diff input is the tips of both branches. You will have the same result if the dots are left out and space is used between the branches. Besides, there is a three-dot operator:

git diff branch1...branch2

The three-dot operator changes the first input parameter branch1 initiating the diff. It transforms branch1 into a ref of the shared common ancestor commit between the two diff inputs. The last input parameter stays the same as the tip of branch2. (三点运算符更改启动diff的第一个输入参数branch1。它将branch1转换为两个diff输入之间共享的共同祖先提交的ref。最后一个输入参数与branch2的尖端保持相同。)

Comparison of the files from two branches

Comparison of the files from two branches (两个分支文件对比)

In order to compare a specific file in branches, you should pass in the path of the file as the third argument to git diff:

git diff master new_branch ./test.txt


请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部