git 回购中缺少行

Missing line in git repo

我的 git 存储库发生了一些奇怪的事情。
正如您从以下差异中看到的那样,首先我们添加了 console.log(config),在下一次提交中我们删除了它,在最后一次我们再次删除了它,因为该行仍然存在于 repo 中。

这怎么可能?也许合并发生了一些奇怪的事情?

commit e28e546f003f4b55cc508e60b8a2c7ba3ad1ffab
Date:   Thu Jan 21 14:46:50 2021 +0100

    remove console log

diff --git a/lib/config.js b/lib/config.js
index 6f20b32..ba26ac3 100644
--- a/lib/config.js
+++ b/lib/config.js
@@ -293,8 +293,6 @@ for (let c in config) {
 
 config.i18n.supportedLocales = (config.i18n.locales).filter(l => l.iso === locale)
 
-console.log(config)
-
 const exportedConfig = {
   config: config,
   server: {

commit fc05a16cda8cd9f72d73b995af001dd4ef932a70
Merge: 5b1ef6e 86e1a6a
Date:   Wed Jan 20 17:53:14 2021 +0000

    Merge branch 'development' into 'staging'
    
    Development --> Staging
    
    See merge request !681

commit 86e1a6aaec44c65e89e0507caf463876d9323d86
Date:   Wed Jan 20 18:50:03 2021 +0100

    Fool GIT

diff --git a/lib/config.js b/lib/config.js
index 29d0c26..ba26ac3 100644
--- a/lib/config.js
+++ b/lib/config.js
@@ -289,6 +289,8 @@ for (let c in config) {
   }
 }
 
+/// console.log(config)
+
 config.i18n.supportedLocales = (config.i18n.locales).filter(l => l.iso === locale)
 
 const exportedConfig = {

commit fc85ca3069f9d76072a0488cade1477e47844e8d
Date:   Wed Jan 20 18:45:17 2021 +0100

    Remove console.log()

diff --git a/lib/config.js b/lib/config.js
index f2f295c..29d0c26 100644
--- a/lib/config.js
+++ b/lib/config.js
@@ -291,8 +291,6 @@ for (let c in config) {
 
 config.i18n.supportedLocales = (config.i18n.locales).filter(l => l.iso === locale)
 
-console.log(config)
-
 const exportedConfig = {
   config: config,
   server: {

commit f2d09dc045dd3e536a09929403e067c5c8fb8b62
Date:   Wed Jan 20 18:43:39 2021 +0100

    Restore console.log()

diff --git a/lib/config.js b/lib/config.js
index 29d0c26..f2f295c 100644
--- a/lib/config.js
+++ b/lib/config.js
@@ -291,6 +291,8 @@ for (let c in config) {
 
 config.i18n.supportedLocales = (config.i18n.locales).filter(l => l.iso === locale)
 
+console.log(config)
+
 const exportedConfig = {
   config: config,
   server: {

该行已添加到合并提交中。如果你 checkout 提交并打开 lib/config.js 你会在那里看到这些行。

那么,为什么这些行不显示在 diff 中?这很混乱,我会同意你的。这是因为 diff 不显示合并提交的差异。

每当它进行差异化时 git 需要知道哪些提交正在差异化。大多数提交都有一个父项,因此差异存在于每个提交与其父项之间。但是,合并提交有多个父项。它应该与哪个不同?有多个选项可供选择 git 并不知道如何显示有用的差异,所以它只显示提交消息。

有关更深入的解释,请参阅 this excellent answer:

Commits are not diffs; commits are snapshots. This might seem like a distinction without a difference—and for some commits, it is. But for merge commits, it's not.

When git show (or git log -p) shows a commit as a diff, it's doing so by comparing the commit's snapshot to something else. The git diff command does the same thing: it compares one commit to another commit. (Or it can compare a commit to the work-tree, or to the contents of the index, or a few other combinations as well.)

For ordinary commits, it's trivially obvious what to compare: compare this commit's snapshot to the previous (i.e., parent) commit's snapshot. So that is what git show does (and git log -p too): it runs a git diff from the parent commit, to this commit.

Merge commits don't have just one parent commit, though. They have two parents.1 This is what makes them "merge commits" in the first place: the definition of a merge commit is a commit with at least two parents.