git diff 提交不显示所有更改文件的差异

git diff commits do not shows all the changed files' difference

当我从master分支合并一个分支(wxc):

$ git merge wxc
Merge made by the 'recursive' strategy.
 Q2/__init__.py                                                                |   3 +
 Q2/common/GLOBAL_CONST.py                                                     |   8 +--
 Q2/urls.py                                                                    |   7 ---
 .../admin_productconfig_physicalserver/api/paginations.py"                   |   1 +
 .../admin_productconfig_physicalserver/api/serializers.py"                   |  22 ++++++-
 .../admin_productconfig_physicalserver/api/switch_cmd_util.py"               |  31 +---------
 .../admin_productconfig_physicalserver/api/urls.py"                          |   6 ++
 .../admin_productconfig_physicalserver/api/views.py"                         | 133 +++++++++++++++++++++++++++++++++++++++-
 .../admin_productconfig_physicalserver/migrations/0091_mg.py"                |  34 ++++++++++
 .../admin_productconfig_physicalserver/migrations/0092_mgnumber.py"          |  25 ++++++++
 .../migrations/0093_auto_20190902_1249.py"                                         |  19 ++++++
 .../migrations/0094_merge_20190906_0822.py"                                        |  16 +++++
 .../admin_productconfig_physicalserver/models.py"                            |  61 ++++++++++++++++--
 .../admin_usermanage/api/serializers.py"                                     |   2 +-
 .../admin_usermanage/api/views.py"                                           |   4 +-
 15 files changed, 318 insertions(+), 54 deletions(-)
 create mode 100644 "677421761706510506570/427531155756/qiyun_admin_productconfig_physicalserver/migrations/0091_mg.py"
 create mode 100644 "677421761706510506570/427531155756/qiyun_admin_productconfig_physicalserver/migrations/0092_mgnumber.py"
 create mode 100644 "677421761706510506570/427531155756/qiyun_admin_productconfig_physicalserver/migrations/0093_auto_20190902_1249.py"
 create mode 100644 "677421761706510506570/427531155756/qiyun_admin_productconfig_physicalserver/migrations/0094_merge_20190906_0822.py"

你看有很多文件变化,比如Q2/__init__.pyQ2/common/GLOBAL_CONST.pyQ2/urls.py等等:

然后我查看我的 master brach 的最新两个日志:

$ git log -n2
commit b63c3856677916f8008b338c139bd3b00c90593d (HEAD -> master)
Merge: 6b59b80 3e85a8d
Author: dale <dale@gmail.com>
Date:   Wed Sep 18 11:28:25 2019 +0800

    Merge branch 'wxc 20190917-wxc'

commit 3e85a8dfd0270f6fb0f3786b8761038df322890f (remote_repo/wxc, wxc)
Author: dale <dale@gmail.com>
Date:   Tue Sep 17 16:04:54 2019 +0800

    '2019-09-17-wxc-01'

我想比较最近两次提交的区别:

dale-MBP:Q2 ldl$ git diff 3e85a8df b63c3856
diff --git "a/677421761706510506570/427531155756/admin_productconfig_physicalserver/api/whmcs/views.py" "b/677421761706510506570/427531155756/admin_productconfig_physicalserver/api/whmcs/views.py"
index ccc1ba3..9fac902 100644
--- "a/677421761706510506570/427531155756/admin_productconfig_physicalserver/api/whmcs/views.py"
+++ "b/677421761706510506570/427531155756/admin_productconfig_physicalserver/api/whmcs/views.py"
@@ -74,8 +74,11 @@ class WHMCSPhysicalServerIPMIManagementAPIView(APIView):

             if whmcs_site not in [
                 "www.44.hk",
+                "44.hk",
                 "www.44.com",
-                "www.44.net"
+                "44.com",
+                "www.44.net",
+                "44.net"
             ]:
                 return Response(status=HTTP_400_BAD_REQUEST, data="error")

@@ -100,6 +103,8 @@ class WHMCSPhysicalServerIPMIManagementAPIView(APIView):
             physical_server_name = request.data.get('physical_server_name')
             action = request.data.get('action')

+            physical_server_name = str(physical_server_name).split("/")[0]
+
             if not physical_server_name or not action:
                 return Response(data="invalid request data", status=HTTP_400_BAD_REQUEST)

你看只比较了一个文件(admin_productconfig_physicalserver/api/whmcs/views.py),为什么不比较其他文件呢?另一个问题是 admin_productconfig_physicalserver/api/whmcs/views.pymerge 文件更改列表中不存在:

 Q2/__init__.py                                                                |   3 +
 Q2/common/GLOBAL_CONST.py                                                     |   8 +--
 Q2/urls.py                                                                    |   7 ---
 .../admin_productconfig_physicalserver/api/paginations.py"                   |   1 +
 .../admin_productconfig_physicalserver/api/serializers.py"                   |  22 ++++++-
 .../admin_productconfig_physicalserver/api/switch_cmd_util.py"               |  31 +---------
 .../admin_productconfig_physicalserver/api/urls.py"                          |   6 ++
 .../admin_productconfig_physicalserver/api/views.py"                         | 133 +++++++++++++++++++++++++++++++++++++++-
 .../admin_productconfig_physicalserver/migrations/0091_mg.py"                |  34 ++++++++++
 .../admin_productconfig_physicalserver/migrations/0092_mgnumber.py"          |  25 ++++++++
 .../migrations/0093_auto_20190902_1249.py"                                         |  19 ++++++
 .../migrations/0094_merge_20190906_0822.py"                                        |  16 +++++
 .../admin_productconfig_physicalserver/models.py"                            |  61 ++++++++++++++++--
 .../admin_usermanage/api/serializers.py"                                     |   2 +-
 .../admin_usermanage/api/views.py"                                           |   4 +-
 15 files changed, 318 insertions(+), 54 deletions(-)
 create mode 100644 "677421761706510506570/427531155756/qiyun_admin_productconfig_physicalserver/migrations/0091_mg.py"
 create mode 100644 "677421761706510506570/427531155756/qiyun_admin_productconfig_physicalserver/migrations/0092_mgnumber.py"
 create mode 100644 "677421761706510506570/427531155756/qiyun_admin_productconfig_physicalserver/migrations/0093_auto_20190902_1249.py"
 create mode 100644 "677421761706510506570/427531155756/qiyun_admin_productconfig_physicalserver/migrations/0094_merge_20190906_0822.py"

当您 运行 git diff 3e85a8df b63c3856 时,您没有查看两个分支之间的合并差异。相反,您正在查看两个父分支之一所做的最后更改(以恰好包含最新更改的那个为准)。

这是因为合并提交 (b63c3856) 仅用于通过捕获两个父提交和日志消息来记录合并。来自 git-merge description:

Then "git merge topic" will replay the changes made on the topic branch since it diverged from master (i.e., E) until its current commit (C) on top of master, and record the result in a new commit along with the names of the two parent commits and a log message from the user describing the changes.

需要注意的关键是"replay the changes"。来自每个分支的所有单独提交都被保留。

在您的 git log 输出中,注意描述合并提交的输出中的 Merge 字段:

Merge: 6b59b80 3e85a8d

这些是合并前每个分支的最后两次提交的提交哈希。您可以使用以下命令显示分支之间的差异:

git diff 6b59b80...3e85a8d

来自git diff documentation:

git diff [<options>] <commit>...<commit> [--] [<path>…​]

This form is to view the changes on the branch containing and up to the second <commit<, starting at a common ancestor of both .

git log 输出看成图表也可能是说明性的:

$ git log --graph --oneline --all

或者,如果您在合并时使用了 --squash 标志,则行为会符合您的预期。来自 git-merge --squash flag documentation(强调我的):

Produce the working tree and index state as if a real merge happened (except for the merge information), but do not actually make a commit, move the HEAD, or record $GIT_DIR/MERGE_HEAD (to cause the next git commit command to create a merge commit). This allows you to create a single commit on top of the current branch whose effect is the same as merging another branch (or more in case of an octopus).

合并提交记录合并分支的组合更改,以及对合并内容提示处的提交的引用。

man git-merge浓缩:

给定:

      A---B---C topic
     /
D---E---F---G master

...将 topic 合并到 master 将得到:

      A---B---C topic
     /         \
D---E---F---G---H master

CommitH,合并提交,记录合并topicmaster的结果。它的 first parent 将是 Gmaster 合并前的提交)。它的 second parent 将是 C (最后一次提交 topic)。

在您的日志中显示在合并提交下的提交是合并提交的 second parent,即您合并的分支的最后一次提交(C 在上面的例子中)。要将 master 分支与其合并前的状态进行比较(上例中的 G),请与合并前 master 中的最后一次提交进行比较。这将是第一个 parent.

git diff master^1 master

作为 shorthand,您可以省略 1,因为它暗示:

git diff master^ master

有关在 man gitrevisions 上指定修订的更多信息。