要比较两个Git分支之间的差异,可以使用以下命令:
git diff <branch1>..<branch2>
其中,<branch1>
和<branch2>
是要比较的两个分支名称。使用 ..
来表示这两个分支之间的差异。
执行上述命令后,Git将会显示出这两个分支之间的不同之处。如果想要更详细的信息,可以加上 -p
参数来显示每个变更的补丁(patch)。
例如,以下是比较名为 feature-branch
和 master
分支之间的不同之处,并将其中的关键词高亮。
git diff feature-branch..master -p --word-diff=color
此命令将会输出一些类似于下面的内容:
diff --git a/file.txt b/file.txt
index 0123456..abcdefg 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,3 @@
This is the original content of file.txt.
-Now we are adding some new content to this file.
+Now we are changing this line to test Git diff.
This will be the third line in the file.
其中,-p
表示要输出补丁,--word-diff=color
表示以颜色高亮的形式显示差异(增/删的单词以绿色/红色表示)。
需要注意的是,Git diff 命令只会比较文件的内容,而不会比较文件的元数据(例如文件权限、所有权等)。如果需要比较元数据,请使用 git difftool
命令。