Cyberithub

Solved "Automatic merge failed; fix conflicts and then commit the result"

Advertisements

In this article, we will see how to solve "Automatic merge failed; fix conflicts and then commit the result" error. Sometimes you may have observed that while trying to pull and merge the changes from a remote release branch to a local branch, you get this "Automatic merge failed; fix conflicts and then commit the result" error for some of the conflicting changes detected in the files and folders. This error usually occurs when git unable to decide which changes to keep and which one to ignore during merge operation. So it is up to you decide and keep the changes as required.

You can do this by various ways but here I will explain you the most appropriate way which always works and you should always use this way in critical or production systems. Since I myself always use this approach to solve this "Automatic merge failed; fix conflicts and then commit the result" error so I would also recommend you to follow this approach.

 

Solved "Automatic merge failed; fix conflicts and then commit the result"

Solved "Automatic merge failed; fix conflicts and then commit the result"

Also Read: How to Remove Application Access to Your GitHub Account

Before going to the solution, I will explain this error further for better understanding by taking an example of my case. So in my case, I have two branches in hcm repo - master and rel-9.7. I have already cloned the repo in my local system and currently on master branch. Here I am trying to merge all the latest changes available in rel-9.7 branch with master. But as soon as I ran git pull origin rel-9.7 --no-ff command to pull and merge all the changes from remote rel-9.7 branch, I noticed "Automatic merge failed; fix conflicts and then commit the result" error on the output as you can also see below.

cyberithub@macos1066 ~ % git pull origin rel-9.7 --no-ff
warning: Pulling without specifying how to reconcile divergent branches is 
discouraged. You can squelch this message by running one of the following
commands sometimes before your next pull:

 git config pull.rebase false # merge (the default strategy)
 git config pull.rebase true  # rebase
 git config pull.ff only      # fast-forward only

You can replace "git config" with "git config --global" to set a default
preference for all repositories. You can also pass --rebase, --no-rebase, 
or --ff-only on the command line to override the configured default per
invocation.

From https://apps.cyberithub.com/scm/hcm/sample-service
*branch        rel-9.7 -> FETCH_HEAD
Auto-merging app/config/service/config.yaml
CONFLICT(content): Merge conflict in app/config/service/config.yaml
Auto-merging app/config/service/resource.yaml
CONFLICT(content): Merge conflict in app/config/service/resource.yaml
Automatic merge failed; fix conflicts and then commit the result.

To verify the conflict, when I opened above problematic files to see the conflicting marker, then I noticed below changes were conflicting.

cyberithub@macos1066 ~ % cat app/config/service/config.yaml
....................
<<<<<<<< HEAD
   version: 1.98.7
======== 
   version: 1.100.5
>>>>>>>> c79t1d0982ba98695cd38596a43ef54
.....................

Similarly, when I checked app/config/service/resource.yaml file, I noticed conflicting marker pointing to below cpu value in resource.yaml file.

cyberithub@macos1066 ~ % cat app/config/service/resource.yaml
....................
<<<<<<<< HEAD
    cpu: 2
======== 
    cpu: 4
>>>>>>>> c79t1d0982ba98695cd38596a43ef54
....................

While the above error can be fixed by multiple ways, here we will use the most suitable way which is by doing all the appropriate changes manually. For example, here we have fixed the error by providing the required values and removing the conflicting marker from the file as you can see below. It is also important to understand here that if you want, you can keep both the changes depending on your needed configuration. In our case, we are just keeping the latest required changes and removing the old ones.

cyberithub@macos1066 ~ % vi app/config/service/config.yaml
....................
version: 1.100.5
.....................

Similarly, we manually provided required value in app/config/service/resource.yaml file as well as you can see below.

cyberithub@macos1066 ~ % vi app/config/service/resource.yaml
....................
cpu: 4
.....................

After doing the above changes when you check the git status, you will see two of the modified files as shown below.

cyberithub@macos1066 ~ % git status
On branch master
Your branch is up to date with 'origin/master'.

You have unmerged paths.
 (fix conflicts and run "git commit")
 (use "git merge --abort" to abort the merge)

Changes to be committed:
   modified: app/config/service/server.properties
   modified: app/config/service/plugin.yaml
   modified: app/config/service/env.yaml

Unmerged paths:
 (use "git add <file>..." to mark resolution)
    both modified: app/config/service/config.yaml
    both modified: app/config/service/resource.yaml

You have to add the modified files and then proceed towards committing them. So to add all the files run git add . command from the root path as shown below. You can also add files manually by providing its name using git add <file_name> command.

cyberithub@macos1066 ~ % git add .

Then commit all the changes using git commit command by providing appropriate commit message as shown below.

cyberithub@macos1066 ~ % git commit -m "Fixed conflict error"
[master e733d63c] Fixed conflict error

Now if you run git status command again then you will notice that this time it won't show anything else to commit.

cyberithub@macos1066 ~ % git status
On branch master
Your branch is ahead of 'origin/master' by 4 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

So now that conflict issue is resolved, let's try to pull the changes again from same remote release branch into local master and see all the changes are pulled by running same git pull origin rel-9.7 --no-ff command again as shown below.

cyberithub@macos1066 ~ % git pull origin rel-9.7 --no-ff
warning: Pulling without specifying how to reconcile divergent branches is
discouraged. You can squelch this message by running one of the following
commands sometimes before your next pull:

git config pull.rebase false # merge (the default strategy)
git config pull.rebase true # rebase
git config pull.ff only # fast-forward only

You can replace "git config" with "git config --global" to set a default
preference for all repositories. You can also pass --rebase, --no-rebase,
or --ff-only on the command line to override the configured default per
invocation.

From https://apps.cyberithub.com/scm/hcm/sample-service
*branch rel-9.7 -> FETCH_HEAD
Already up to date.

Since all the changes from remote release branch are pulled and merged with local master branch successfully, it is time to publish these changes to remote master branch by running git push origin master command as shown below.

cyberithub@macos1066 ~ % git push origin master

You will notice that all the changes will be pushed to remote master branch without any error. It is also worth mentioning here that while above method works fine if you have merge conflict error in few files but in case if you observe the same error happening in probably tens and hundreds of files or may be on entire files in repo then fixing it manually may not be the ideal way to deal with the error. In that case, you have to check your branch itself for any other possible problems and take appropriate measures. Hope this helped solve your error.

Leave a Comment