Search code examples
gitgit-mergegit-diff

Missing line in git repo


Something weird happened to my git repo.
As you can see from the following diff, at first we added console.log(config), in the next commit we removed it and in the last we removed it again because that line was still present on the repo.

How's that possible? Maybe something weird happened with the merge?

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: {

Solution

  • The line was added in the merge commit. If you checkout the commit and open lib/config.js you'll see the lines there.

    Why don't the lines show up in the diff, then? It's confusing, I'll grant you. It's because diff doesn't show diffs of merge commits.

    Whenever it's doing a diff git needs to know what commits are being diffed. Most commits have a single parent and so the diff is between each commit and its parent. Merge commits, however, have multiple parents. Which should it diff against? With multiple options to choose from git doesn't really know how to display a useful diff, so it just shows the commit message.

    For a more in-depth explanation see 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.