Git consistently merging one file incorrectly

320 views Asked by At

I'm having a weird issue with git's merge feature that I've never seen before. I am merging some changes from a branch into master and git seems to be consistently having trouble with one particular file.

It's basically merging content into seemingly random places. It's happened the last few times this file has been merged.

Here are the changes I made to MyFile.cs on the other branch (called somebranch):

-                service.UpdateUser(user);
+                service.UpdateUser(user, false);
...
-                User[] users = client.ImportPersonnel(a, b, c);
+                UserImportResults results = client.ImportPersonnel(a, b, c);
...
-                Response.Write(JsonConvert.SerializeObject(new { UserCount = users.Length }));
+                Response.Write(JsonConvert.SerializeObject(new { Added = results.NumAdded, Updated = results.NumUpdated }));

Just a few lines of code.

So then I did this:

git merge somebranch master

Most of it is merged without conflicts. A few files do have conflicts but they make sense. But the automerge changes it made to MyFile.cs are strange. For example, the line

service.UpdateUser(user, false);

was stuck in some random place in the middle of the master version of MyFile.cs:

                if (Request["X"] != null)
                {
                    CalculateSomething(Request["X"]);
                }

<<<<<<< HEAD
                if (Request["Y"] != null)
                {
                    CalculateSomething(Request["Y"]);
                }
=======
            try
            {
                client.UpdateUser(user, false);
>>>>>>> qaqc-release

                if (Request["Z"] != null)
                {
                    CalculateSomething(Request["Z"]);
                }

I'm not sure how git determines how to merge two files together but I haven't seen it do this before. And it only happens with this one file.

The place where it should be merging the change in and the place where it's actually doing it are a little over 100 lines apart. However, this file is almost 5000 lines long so relatively speaking it's not too far off but still it's odd.

Any ideas why this is happening or what I can do about it? This is an often-touched file so it's going to be constantly merged. It's never been an issue in the last year or so of development so I'm not sure what changed but it would be great to address it.

1

There are 1 answers

2
Phil Miller On

Two things to check:

  • Since this is an oft-merged file, perhaps there was some incorrect merge performed and then backed out in the past, that's been recorded by git rerere. Maybe look in meta-data there if you have that enabled?
  • Your code formatting may actually be throwing things off. If the file has so many places that can match the beginning of a patch, especially one that's only adding new lines, I could easily see it getting confused if it doesn't consider enough context. Maybe there's a flag to git merge that will tell it to look at a broader range of context than it does by default, to prevent mistaken matches?