Home > Blockchain >  How to resolve easy merge conflicts automatically in Git
How to resolve easy merge conflicts automatically in Git

Time:06-07

My code locally:

    disableCaching()
    setupGoogleMaps()
   

Change from remote:

    disableCaching()
    FirebaseApp.configure()
    initializeNotificationService()
    setupGoogleMaps()

For me, it seems obvious that two lines have been added above setupGoogleMaps()

But why does git prompt me to fix this conflict manually?

enter image description here

Is there a tool that can fix those easy conflicts automatically?

I already tried the magic icon for resolving simple conflicts but this is not working in this case.

enter image description here

I was able to reproduce it with this repo:

https://github.com/tolotrasamuel/easy-conflict

  1. git checkout featurebranch

  2. git pull origin/master

CodePudding user response:

Is there a tool that can fix those easy conflicts automatically?

Not really, no. The reason Git is turning to you for help is exactly that "automatically" performing the merge isn't working.

The reasons why Git needs help here when you think it shouldn't, is that Git is taking more than one line into account in deciding where the hunks are. You compare

disableCaching()
setupGoogleMaps()

With

disableCaching()
FirebaseApp.configure()
initializeNotificationService()
setupGoogleMaps()

And you say "it seems obvious that two lines have been added". But:

  • Those are different and equally weighted contributions to the merge; i.e., it is not the case that the second one is laid on top of the first, but rather that both of them have to be laid somehow on top of what was originally present, which was just the line disableCaching.

  • To Git, the single pair of lines in the first code counts as just one hunk — and, seen that way, it is clear different from the same hunk in the second code.

My advice in this situation would be:

  1. Don't look for a "tool". Just resolve the conflict and move on. And

  2. Don't use a GUI for this, just jump in and edit the conflicted text file by hand. I think you'll find that's a lot easier. And it's easier to understand, too.

Here's what we get using the diff3 mode:

setupInstabug()
setupAppCenter()
<<<<<<< HEAD
disableCaching()
setupGoogleMaps()
||||||| a19174f
disableCaching()
=======
disableCaching()
FirebaseApp.configure()
initializeNotificationService()
setupGoogleMaps()
>>>>>>> origin/master

The bit in the middle is what we originally had, namely, just the line disableCaching. Okay, so one branch, yours, wants to put

setupGoogleMaps()

after that. The other branch, master, wants to put

FirebaseApp.configure()
initializeNotificationService()
setupGoogleMaps()

And you're saying, fine, so why not just do both?

But to Git, there is no "both": those are two completely different things that might be added after your line, because disableCaching is a unit together with the following line. It doesn't see this the way you do. It's saying, "Which did you want: the one line, or the three lines?" So since you know what's meant — namely, the three lines — simply edit the file to say so:

setupInstabug()
setupAppCenter()
disableCaching()
FirebaseApp.configure()
initializeNotificationService()
setupGoogleMaps()

Now say git add . and then git merge --continue and you're all set.

  • Related