Find the Differences Between Two Git Branches – 查找两个Git分支之间的差异

最后修改: 2022年 7月 28日

1. Overview

1.概述

In this tutorial, we’ll discover ways of finding differences between two git branches. We are going to explore the git diff command and use it in branch comparison.

在本教程中,我们将发现寻找两个git分支之间差异的方法。我们将探索git diff命令,并在分支比较中使用它。

2. Compare Branches in a Single Command

2.在一个单一的命令中比较分支

git diff is a useful command that allows us to compare different types of git objects, such as files, commits, branches, and many more. This makes git diff a good choice when we need to compare the differences between two branches.

git diff是一个有用的命令,它允许我们比较不同类型的git对象,如文件、提交、分支等等。这使得git diff在我们需要比较两个分支之间的差异时成为一个不错的选择。

To compare the branches, we specify both branches’ names after the git diff command:

为了比较各分支,我们在git diff命令后指定两个分支的名称。

$ git diff branch1 branch2 
diff --git a/file1.txt b/file1.txt
index 3b18e51..c28f4fa 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1 +1 @@
-hello world
+hello from branch2

Let’s examine the output. The first line diff ‐‐git a/file1.txt b/file1.txt shows which files are different in the branches. In our example, it is the file file1.txt, which is represented as a/file1.txt for branch1, and b/file1.txt is the same file for branch2.

让我们检查一下输出结果。第一行 diff –git a/file1.txt b/file1.txt 显示了哪些文件在各分支中是不同的。在我们的例子中,是文件file1.txt,分支1中表示为a/file1.txtb/file1.txt分支2的同一文件。

The next line shows index cache numbers for the file1.txt in both the branches. Then, there are several lines that show the file names with added content, and the ones with removed content.

下一行显示了两个分支中的file1.txt的索引缓存编号。然后,有几行显示添加了内容的文件名,以及删除了内容的文件名。

And finally, the most important lines are at the end of the output, where we can see the exact modified lines. We can see that the line “hello world” in the file file1.txt from branch1 was replaced by the line “hello from branch2” in branch2.

最后,最重要的几行是在输出的最后,我们可以看到确切的修改行。我们可以看到,文件 file1.txtf中的“hello world”branch2中的“hello from branch2”所取代。

3. Showing Only the Names of Files

3.只显示文件的名称

Although we found the differences between the two branches in the example above, it can be inconvenient to display changes for every single line. Often, we want to see just the names of the changed files.

尽管我们在上面的例子中发现了两个分支之间的差异,但要显示每一行的变化是很不方便的。通常情况下,我们只想看到被修改的文件的名称。

To display only the names of files that are different between two branches, we use the  ‐‐name-only option in the git diff command:

为了只显示两个分支之间不同的文件名,我们在git diff命令中使用–name-only选项

$ git diff branch1 branch2 --name-only
file1.txt

Now, the output shows just the name of files that are different in both the branches. In our case, it’s just a single file file1.txt.

现在,输出结果只显示两个分支中不同的文件名。在我们的例子中,它只是一个文件file1.txt

4. An Alternative Way to Compare Branches

4.比较分支机构的另一种方法

In the examples above, we used a single git diff command to find the differences between the branches. However, we had to specify both branches’ names in this command. Sometimes it feels more intuitive if we do the comparison from within one of the branches.

在上面的例子中,我们只用了一条git diff命令来查找各分支之间的差异。然而,我们必须在这个命令中指定两个分支的名称。有时,如果我们从其中一个分支中进行比较,感觉会更直观。

To check out to a branch, we use the git checkout command. After that, we only need to diff the other branch and see all the changes relative to our current branch.

要签出一个分支,我们使用git checkout命令。之后,我们只需要diff另一个分支,就可以看到相对于我们当前分支的所有变化。

For example, let’s compare branch1 and branch2 from within branch2:

例如,让我们从branch1branch2内比较branch2

$ git checkout branch2 
$ git diff branch1

We should now be able to see the same output as earlier.

现在我们应该能够看到与先前相同的输出。

5. Finding the Differences From a Common Ancestor

5.从共同的祖先中寻找不同之处

So far we were comparing the two branches in regard to their latest states. However, sometimes we need to compare a branch with a common ancestor of another branch.

到目前为止,我们是就两个分支的最新状态进行比较。然而,有时我们需要将一个分支与另一个分支的共同祖先进行比较。

In other words, we may need to find all differences in the branch compared to another one since they were branched off. For illustration, let’s see the tree below:

换句话说,我们可能需要找到这个分支与另一个分支相比的所有差异,因为它们是被分支出来的。为了说明问题,让我们看看下面这棵树。

---A---B---C---D  <== branch1
        \
         E---F    <== branch2

We’d like to find the differences between branch2 (current commit position F) and branch1 at its commit position B. As we can see, at position B, the two branches are forked out and then they develop separately from each other. In this case, position B is the common ancestor of the two branches.

我们想找出branch2(当前提交位置F)和branch1在其提交位置B的差异。正如我们所看到的,在位置B,两个分支被分叉出来,然后它们彼此分开发展。在这种情况下,位置B是这两个分支的共同祖先。

To find the diff between branch2 and branch1 common ancestor, we need to use three dots between the branch names:

为了找到branch2branch1共同祖先之间的差异,我们需要在分支名称之间使用三个点。

git diff branch1...branch2

The output will be in a similar format as earlier. However, now the comparison is happening between branch2 and a common ancestor of branch1.

输出将采用与先前类似的格式。然而,现在比较的是branch2branch1的一个共同祖先。

6. Finding the Differences Between Commit Hashes

6.寻找承诺哈希值之间的差异

It’s possible to display differences not only between the branches, but also between the commits.

不仅可以显示各分支之间的差异,还可以显示各提交之间的差异。

The syntax for this is similar to the branch comparison. However, instead of branches, we need to specify the commit hashes that we want to compare:

它的语法与分支比较类似。然而,我们需要指定我们想要比较的提交哈希值,而不是分支。

git diff b94a88bac17318fb3c3cc881d657c04de9fd7901 73ea8956375c10fe41c669ba8c6f6f9e01490452

The output will be of a similar format to the examples above.

输出的格式将与上面的例子类似。

7. Conclusion

7.结语

In this article, we learned how to find the differences between two git branches using the git diff command.

在本文中,我们学习了如何使用git diff命令找到两个git分支之间的差异。

We first looked at comparing the branches using a single command. Then, we found out how to compare branches from within one of the branches using the git checkout command.

我们首先研究了用一条命令比较各分支的情况。然后,我们发现了如何使用git checkout命令从一个分支内比较分支。

Lastly, we saw how to compare branches to a common ancestor, along with finding the differences between commits.

最后,我们看到了如何将分支与一个共同的祖先进行比较,同时找到提交之间的差异。