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

最后修改: 2022年 7月 28日

1. Overview


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


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.


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


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

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.


4. An Alternative Way to Compare Branches


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:


$ 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


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.


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


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.


6. Finding the Differences Between Commit Hashes


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


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.