Cyberithub

Solved "CONFLICT (modify/delete): 'file' deleted in HEAD and modified" error

Advertisements

In this article, we will see how to solve "CONFLICT (modify/delete): file deleted in HEAD and modified" error in git version control. If you are getting this error then I am pretty sure you are either trying to pull the changes from remote branch to current branch or performing rebase operation. This error usually comes when someone deleted the files in current branch but the same is not done in the remote branch from where the changes are getting pulled to merge with the current branch. In that case, it will show "CONFLICT (modify/delete): file deleted in HEAD and modified" error.

If this is the case with you then you can fix the error by following the solution which deemed fit in your scenario. I am going to explain this error by using a real world example which basically happened with me few days back and then the different solutions that you can take based on your use case.

 

Solved "CONFLICT (modify/delete): 'file' deleted in HEAD and modified" error

Solved "CONFLICT (modify/delete): 'file' deleted in HEAD and modified" error

Also Read: [Solved]: "error: Pulling is not possible because you have unmerged files"

To give you a glimpse of my branching strategy, I have basically three branches - develop, master and app.1.22. develop and master branch always remain in sync and only cloning of develop branch is allowed. So i cloned it using git clone <repo_url> and then switched to master using git checkout -b master command. Now, as per our branching strategy feature branch app.1.22 needs to be first merged with master and then with develop to keep both in sync. While I was trying to merge changes from my feature branch to the master, I noticed below error:-

cyberithub@macos1066 ~ % git clone https://apps.cyberithub.com/scm/hcm/app-example
cyberithub@macos1066 ~ % cd app-example
cyberithub@macos1066 app-example % git branch
* develop
cyberithub@macos1066 app-example % git checkout -b master
Switched to a new branch 'master'
cyberithub@macos1066 app-example % git pull origin app.1.22 --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/app-example
*branch        app.1.22 -> FETCH_HEAD
CONFLICT (modify/delete):  app/config/service/config.yaml deleted in HEAD and modified in 78cb9ff2f7a64bd651ca.
Version 78cb9ff2f7a64bd651ca of app/config/service/config.yaml left in tree.
CONFLICT (modify/delete):  app/config/service/resource.yaml deleted in HEAD and modified in 78cb9dd2a7a31bd391df.
Version 78cb9ff2f7a64bd651ca of app/config/service/resource.yaml left in tree.

After checking above error for a while, I found that the files were deleted from develop branch but it was still there in master branch. So when I was trying to merge files from my feature branch app.1.22 to master, it was showing me above error. This is because feature branch app.1.22 has been cut out from develop branch only so it carries all its changes. This can further be confirmed by checking the output of git status command. It will show the merged as well as unmerged path in master branch.

cyberithub@macos1066 app-example % git status
On branch 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/meta/script.yaml
        modified:  app/config/meta/customers.yaml

Unmerged paths:
  (use "git add/rm <file>..." as appropriate to mark resolution)
        deleted by us:  app/config/service/config.yaml
        deleted by us:  app/config/service/resource.yaml

To solve above error, there could be two solutions. Either we keep the files or delete it and then commit the changes. Let's understand this in detail.

 

Solution 1: Let's keep the files

If you decided to keep the deleted files then all you need to do is to add all the files by using git add <file_name> command or you can add all the deleted files in one command by using just git add . from the project root path.

cyberithub@macos1066 app-example % git add app/config/service/config.yaml
cyberithub@macos1066 app-example % git add app/config/service/resource.yaml

Then commit the changes using git commit -m "<commit_message>" command as shown below.

cyberithub@macos1066 app-example % git commit -m "Adding all the deleted files"

Finally push all the changes to remote master branch using git push -u origin master command.

cyberithub@macos1066 app-example % git push -u origin master

 

Solution 2: Remove the files

If you decided to remove all the files then all you need to do is to remove it by using git rm <file_name> command as shown below.

cyberithub@macos1066 app-example % git rm app/config/service/config.yaml
cyberithub@macos1066 app-example % git rm app/config/service/resource.yaml

Then commit the changes using git commit -m "<commit_message>" command as shown below.

cyberithub@macos1066 app-example % git commit -m "Adding all the deleted files"

Finally push all the files to remote master branch using git push -u origin master command. This was also the solution applicable for my use case.

cyberithub@macos1066 app-example % git push -u origin master

 

After following above solutions, you will notice that error will be disappeared and all the changes will be in sync between current and remote branches. Hope it helped you understand about the error and how to solve if you encounter the same during git pull or git rebase operation. Please do let me know if you have any query or feedback on this in comment box.

Leave a Comment