* [BUG] git merge reports conflict when two branches add independent methods at the same location
@ 2025-09-12 13:37 Guo Tingsheng
2025-09-12 22:57 ` Elijah Newren
0 siblings, 1 reply; 3+ messages in thread
From: Guo Tingsheng @ 2025-09-12 13:37 UTC (permalink / raw)
To: git@vger•kernel.org
Hello Git developers,
I would like to report a potential issue in Git's merge behavior, where semantically independent changes at the same position are reported as a conflict, even though they could be merged automatically.
Environment:
- git version: 2.43.0
- OS: Ubuntu 24.04 LTS
Steps to reproduce:
1. Start with a file containing only:
public class Calculator {
}
2. On branch A, add a new method `add`:
@@ -1,1 +1,3 @@
public class Calculator {
+ public static double add(double a, double b) {
+ return a + b;
+ }
3. On branch B, add a new method `subtract`:
@@ -1,1 +1,3 @@
public class Calculator {
+ public static double subtract(double a, double b) {
+ return a - b;
+ }
4. Merge branch A and branch B.
Expected result:
- The merge should succeed automatically, producing a file that contains both methods (order does not matter).
For example:
public class Calculator {
public static double add(double a, double b) {
return a + b;
}
public static double subtract(double a, double b) {
return a - b;
}
}
Actual result:
- Git reports a conflict and aborts the merge, requiring manual conflict resolution.
Additional information:
- Although the two changes occur at the same location in the file, they are independent additions with no semantic overlap.
- It would be desirable for Git's merge algorithm to automatically combine such changes, as the final merged state is deterministic and conflict-free.
- This limitation may impact developer productivity in real-world projects where multiple contributors extend the same class or configuration file independently.
Thanks,
Cori
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [BUG] git merge reports conflict when two branches add independent methods at the same location 2025-09-12 13:37 [BUG] git merge reports conflict when two branches add independent methods at the same location Guo Tingsheng @ 2025-09-12 22:57 ` Elijah Newren 2025-09-13 19:24 ` Ben Knoble 0 siblings, 1 reply; 3+ messages in thread From: Elijah Newren @ 2025-09-12 22:57 UTC (permalink / raw) To: Guo Tingsheng; +Cc: git@vger•kernel.org On Fri, Sep 12, 2025 at 6:38 AM Guo Tingsheng <CoriCraft16@outlook•com> wrote: > > Hello Git developers, > > I would like to report a potential issue in Git's merge behavior, where semantically independent changes at the same position are reported as a conflict, even though they could be merged automatically. > > Environment: > - git version: 2.43.0 > - OS: Ubuntu 24.04 LTS > > Steps to reproduce: > 1. Start with a file containing only: > > public class Calculator { > } > > 2. On branch A, add a new method `add`: > > @@ -1,1 +1,3 @@ > public class Calculator { > + public static double add(double a, double b) { > + return a + b; > + } > > 3. On branch B, add a new method `subtract`: > > @@ -1,1 +1,3 @@ > public class Calculator { > + public static double subtract(double a, double b) { > + return a - b; > + } > > 4. Merge branch A and branch B. > > Expected result: > - The merge should succeed automatically, producing a file that contains both methods (order does not matter). > For example: > > public class Calculator { > public static double add(double a, double b) { > return a + b; > } > public static double subtract(double a, double b) { > return a - b; > } > } > > Actual result: > - Git reports a conflict and aborts the merge, requiring manual conflict resolution. > > Additional information: > - Although the two changes occur at the same location in the file, they are independent additions with no semantic overlap. > - It would be desirable for Git's merge algorithm to automatically combine such changes, as the final merged state is deterministic and conflict-free. > - This limitation may impact developer productivity in real-world projects where multiple contributors extend the same class or configuration file independently. Thanks for the report, but how is Git supposed to know that they are independent changes with no semantic overlap? Git doesn't understand the semantics of the files it tracks. It has no idea whether the things being added are functions, or statements/expressions within a function, or college essays, or hand-written recipes, or data dumps, or anything else. It would need to know those semantics, which would probably require something on the level of use of AI to determine that the changes are independent non-competing additions with no semantic overlap and that you want to keep both. Instead, it knows which lines are the same between the two sides, and which have been modified since the common point of history. Without this knowledge about the meaning of the content, assuming both are wanted is wrong. It would mean that all the other cases where two sides each add lines would also end up with both sets of lines, even when those are conflicting lines in a recipe or conflicting statements in a function, or whatever. When both sides modify the same area in different ways, it's a potential conflict that needs to be given back to the user to resolve. ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [BUG] git merge reports conflict when two branches add independent methods at the same location 2025-09-12 22:57 ` Elijah Newren @ 2025-09-13 19:24 ` Ben Knoble 0 siblings, 0 replies; 3+ messages in thread From: Ben Knoble @ 2025-09-13 19:24 UTC (permalink / raw) To: Elijah Newren; +Cc: Guo Tingsheng, git > Le 12 sept. 2025 à 18:57, Elijah Newren <newren@gmail•com> a écrit : > > On Fri, Sep 12, 2025 at 6:38 AM Guo Tingsheng <CoriCraft16@outlook•com> wrote: >> >> Hello Git developers, >> >> I would like to report a potential issue in Git's merge behavior, where semantically independent changes at the same position are reported as a conflict, even though they could be merged automatically. >> >> Environment: >> - git version: 2.43.0 >> - OS: Ubuntu 24.04 LTS >> >> Steps to reproduce: >> 1. Start with a file containing only: >> >> public class Calculator { >> } >> >> 2. On branch A, add a new method `add`: >> >> @@ -1,1 +1,3 @@ >> public class Calculator { >> + public static double add(double a, double b) { >> + return a + b; >> + } >> >> 3. On branch B, add a new method `subtract`: >> >> @@ -1,1 +1,3 @@ >> public class Calculator { >> + public static double subtract(double a, double b) { >> + return a - b; >> + } >> >> 4. Merge branch A and branch B. >> >> Expected result: >> - The merge should succeed automatically, producing a file that contains both methods (order does not matter). >> For example: >> >> public class Calculator { >> public static double add(double a, double b) { >> return a + b; >> } >> public static double subtract(double a, double b) { >> return a - b; >> } >> } >> >> Actual result: >> - Git reports a conflict and aborts the merge, requiring manual conflict resolution. >> >> Additional information: >> - Although the two changes occur at the same location in the file, they are independent additions with no semantic overlap. >> - It would be desirable for Git's merge algorithm to automatically combine such changes, as the final merged state is deterministic and conflict-free. >> - This limitation may impact developer productivity in real-world projects where multiple contributors extend the same class or configuration file independently. > > Thanks for the report, but how is Git supposed to know that they are > independent changes with no semantic overlap? Git doesn't understand > the semantics of the files it tracks. It has no idea whether the > things being added are functions, or statements/expressions within a > function, or college essays, or hand-written recipes, or data dumps, > or anything else. It would need to know those semantics, which would > probably require something on the level of use of AI to determine that > the changes are independent non-competing additions with no semantic > overlap and that you want to keep both. Sorry, can’t resist: we’ve been writing semantic analysis code for decades. It wouldn’t take AI to get this right. What’s hard about the situation is that it doesn’t generalize easily: a semantic analyzer that can resolve conflicts for one file, project, language, etc., is not necessarily of any help to its brethren. Still, if you write a lot of Java or similar code and have this problem frequently, building a merge strategy option or mergetool you can use to resolve this easily seems worthwhile. (Not something mainline Git needs, of course, for all the reasons pointed out here.) ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-09-13 19:24 UTC | newest] Thread overview: 3+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2025-09-12 13:37 [BUG] git merge reports conflict when two branches add independent methods at the same location Guo Tingsheng 2025-09-12 22:57 ` Elijah Newren 2025-09-13 19:24 ` Ben Knoble
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox