# [BUG] git merge sometimes loses changes from one branch

3 messages from 2025-09-12 to 2025-10-28. Participants: Guo Tingsheng, Elijah Newren.
Thread: https://gitlist.dev/t/64135

## Guo Tingsheng, 2025-09-12 13:40

Subject: [BUG] git merge sometimes loses changes from one branch
Message-ID: <SN6PR03MB4285CA9885AE7952EEF762E1AE08A@SN6PR03MB4285.namprd03.prod.outlook.com>
URL: https://gitlist.dev/e/SN6PR03MB4285CA9885AE7952EEF762E1AE08A%40SN6PR03MB4285.namprd03.prod.outlook.com

```
Hello Git developers,

I would like to report a potential issue in Git's merge behavior, where changes from one branch may be lost even when the other branch does not modify the same location.

Environment:
- git version: 2.43.0
- OS: Ubuntu 24.04 LTS

Steps to reproduce:
1. On branch A, modify a file at a certain location:

   @@ -1026,7 +1028,9 @@
   public final class AutoBuilderCompilationTest {
                "  }",
                "}"); 
        Compilation compilation =
   -        javac().withProcessors(new AutoBuilderProcessor()).compile(javaFileObject);
   +        javac()
   +            .withProcessors(new AutoBuilderProcessor())
   +            .compile(javaFileObject);
        assertThat(compilation).failed();
        assertThat(compilation)
            .hadErrorContaining(

2. On branch B, make no changes to this location.

3. Merge branch A and branch B.

Expected result:
- The merge should retain the changes made in branch A:

   @@ -1026,7 +1028,9 @@
   public final class AutoBuilderCompilationTest {
                "  }",
                "}"); 
        Compilation compilation =
   -        javac().withProcessors(new AutoBuilderProcessor()).compile(javaFileObject);
   +        javac()
   +            .withProcessors(new AutoBuilderProcessor())
   +            .compile(javaFileObject);
        assertThat(compilation).failed();
        assertThat(compilation)
            .hadErrorContaining(

Actual result:
- Git sometimes produces a merged result where the modification from branch A is lost, yielding:

   @@ -1026,7 +1028,9 @@
   public final class AutoBuilderCompilationTest {
                "  }",
                "}"); 
        Compilation compilation =
           javac().withProcessors(new AutoBuilderProcessor()).compile(javaFileObject);
        assertThat(compilation).failed();
        assertThat(compilation)
            .hadErrorContaining(

Additional information:
- This issue occurs even when the other branch (B) does not modify the same location.
- It appears to happen non-deterministically and may lead to subtle bugs in real-world merges.

Thanks,
Cori

```

## Elijah Newren, 2025-09-12 22:56

Subject: Re: [BUG] git merge sometimes loses changes from one branch
Message-ID: <CABPp-BGeZ6no5LdBGKjykQLqUHRjFis7ZtGdEwnzg0gy_CkLqA@mail.gmail.com>
URL: https://gitlist.dev/e/CABPp-BGeZ6no5LdBGKjykQLqUHRjFis7ZtGdEwnzg0gy_CkLqA%40mail.gmail.com
In-Reply-To: <SN6PR03MB4285CA9885AE7952EEF762E1AE08A@SN6PR03MB4285.namprd03.prod.outlook.com>

```
On Fri, Sep 12, 2025 at 6:41 AM Guo Tingsheng <CoriCraft16@outlook.com> wrote:
>
> Hello Git developers,
>
> I would like to report a potential issue in Git's merge behavior, where changes from one branch may be lost even when the other branch does not modify the same location.
>
> Environment:
> - git version: 2.43.0
> - OS: Ubuntu 24.04 LTS
>
> Steps to reproduce:
> 1. On branch A, modify a file at a certain location:
>
>    @@ -1026,7 +1028,9 @@
>    public final class AutoBuilderCompilationTest {
>                 "  }",
>                 "}");
>         Compilation compilation =
>    -        javac().withProcessors(new AutoBuilderProcessor()).compile(javaFileObject);
>    +        javac()
>    +            .withProcessors(new AutoBuilderProcessor())
>    +            .compile(javaFileObject);
>         assertThat(compilation).failed();
>         assertThat(compilation)
>             .hadErrorContaining(
>
> 2. On branch B, make no changes to this location.
>
> 3. Merge branch A and branch B.
>
> Expected result:
> - The merge should retain the changes made in branch A:
>
>    @@ -1026,7 +1028,9 @@
>    public final class AutoBuilderCompilationTest {
>                 "  }",
>                 "}");
>         Compilation compilation =
>    -        javac().withProcessors(new AutoBuilderProcessor()).compile(javaFileObject);
>    +        javac()
>    +            .withProcessors(new AutoBuilderProcessor())
>    +            .compile(javaFileObject);
>         assertThat(compilation).failed();
>         assertThat(compilation)
>             .hadErrorContaining(
>
> Actual result:
> - Git sometimes produces a merged result where the modification from branch A is lost, yielding:
>
>    @@ -1026,7 +1028,9 @@
>    public final class AutoBuilderCompilationTest {
>                 "  }",
>                 "}");
>         Compilation compilation =
>            javac().withProcessors(new AutoBuilderProcessor()).compile(javaFileObject);
>         assertThat(compilation).failed();
>         assertThat(compilation)
>             .hadErrorContaining(
>
> Additional information:
> - This issue occurs even when the other branch (B) does not modify the same location.
> - It appears to happen non-deterministically and may lead to subtle bugs in real-world merges.

My only guess here would be having a non-unique merge-base, where one
of the merge bases also introduced this change, so that it appears
that A made no change relative to the consolidated merge base while
also making it look like branch B undid the change relative to that
consolidated merge base.  But, that's a wild guess and I have no idea
if that's related to what you're seeing or if there's some unrelated
issue at play.

Can you create a testcase to reproduce this, either a repository that
others can clone together with the commands to trigger it, or a bunch
of commands to both create a simple repository and demonstrate the
issue?

```

## Guo Tingsheng, 2025-10-28 16:12

Subject: 回复: [BUG] git merge sometimes loses changes from one branch
Message-ID: <SN6PR03MB4285533B67E31D36172A4C34AEFDA@SN6PR03MB4285.namprd03.prod.outlook.com>
URL: https://gitlist.dev/e/SN6PR03MB4285533B67E31D36172A4C34AEFDA%40SN6PR03MB4285.namprd03.prod.outlook.com
In-Reply-To: <CABPp-BGeZ6no5LdBGKjykQLqUHRjFis7ZtGdEwnzg0gy_CkLqA@mail.gmail.com>

```
Hello Git developers,

I would like to report a potential issue in Git’s merge behavior, where
changes from one branch may be lost even when the other branch does not
modify the same location.

To make it easier to reproduce, I have open-sourced this example in a public repository:
Repository: https://github.com/CoriWork/bug-replay.git

You can clone the repository and follow the README.md to replay the bug!

---

Environment:
- Git version: 2.43.0
- OS: Ubuntu 24.04 LTS

---

Steps to reproduce:
1. On branch B, modify a file at a certain location:

@@ -1068,9 +1026,7 @@ public final class AutoBuilderCompilationTest {
             "  }",
             "}");
     Compilation compilation =
-        javac()
-            .withProcessors(new AutoBuilderProcessor())
-            .compile(javaFileObject);
+        javac().withProcessors(new AutoBuilderProcessor()).compile(javaFileObject);
     assertThat(compilation).failed();
     assertThat(compilation)
         .hadErrorContaining(

2. On branch A, make no changes to this location.

3. Merge branch A and branch B.

---

Expected result:
The merge should retain the change made in branch B:

@@ -1068,9 +1026,7 @@ public final class AutoBuilderCompilationTest {
             "  }",
             "}");
     Compilation compilation =
-        javac()
-            .withProcessors(new AutoBuilderProcessor())
-            .compile(javaFileObject);
+        javac().withProcessors(new AutoBuilderProcessor()).compile(javaFileObject);
     assertThat(compilation).failed();
     assertThat(compilation)
         .hadErrorContaining(

Actual result:
Git sometimes produces a merged result where the modification from branch A
is lost, yielding:

   @@ -1026,7 +1028,9 @@
   public final class AutoBuilderCompilationTest {
                "  }",
                "}");
       Compilation compilation =
           javac()
            .withProcessors(new AutoBuilderProcessor())
            .compile(javaFileObject);

---

Additional information:
- This issue occurs even when branch B does not modify the same location.
- It appears to happen deterministically under a specific construction that
  splits one patch into two disjoint sets of hunks, applies them separately
  to create two branches, and then merges them back.
- The merged result can differ from the expected content of the original commit.

Thank you for your time and your work maintaining Git.

Best regards,  
Guo Tingsheng  
< CoriCraft16@outlook.com >

```
