3

I tried to create a new pull request after pushing to my branch, as I want to merge my branch into the main branch. GitHub's response was "This branch has conflicts that need to be resolved". However, the conflicts are so complex that I should resolve them locally. The suggestion for this was:
Step 1: Clone the repository or update your local repository with the latest changes.
git pull origin main
Step 2: Switch to the head branch of the pull request.
git checkout myBrach
Step 3: Merge the base branch into the head branch.
git merge main
Step 4: Fix the conflicts and commit the result.
See Resolving a merge conflict using the command line for step-by-step instructions on resolving merge conflicts.
Step 5: Push the changes.
git push -u origin myBranch

But after git merge main I get the following error:

error: Your local changes to the following files would be overwritten by merge:  
 ... // Files listed  
Merge with strategy ort failed.  

I thought this suggestion will help me to update myBranch and remove the "commits behind main". I don't know what this error Merge with strategy ort failed means and how I can solve this problem without losing code.

kmjln
  • 31
  • 1
  • 1
  • 3
  • Does this answer your question? [git pull fails with "Untracked working tree file 'blah' would be overwritten by merge", but tree is clean](https://stackoverflow.com/questions/3165696/git-pull-fails-with-untracked-working-tree-file-blah-would-be-overwritten-by) – knittl Aug 08 '23 at 17:19
  • Sorry, wrong dupe target. This is the correct one: https://stackoverflow.com/questions/15745045/how-do-i-resolve-git-saying-commit-your-changes-or-stash-them-before-you-can-me – knittl Aug 08 '23 at 17:35

2 Answers2

3

The merge main step is not the recommended best practice.

You might consider instead rebasing your branch on top of merge, and then force pushing your branch (which will update the PR)

By rebasing your branch commits on top of main, you will resolve conflicts locally.

git switch main
git pull origin main

git switch myBranch
git rebase main
# resolve any local conflict

git push --force

With rebasing, conflicts are dealt with one commit at a time. That allows you to understand the context of each conflict, as you are effectively re-writing history to make it seem like your branch's changes were made directly onto the latest version of the main branch.

It avoids unnecessary merge commits in the history. By reapplying commits one by one, you maintain a clean, linear history.

And, regarding the PR, that rebase ensures your changes are compatible with the latest version and provides a clean history without merge commits.

Since a GitHub PR is automatically updated on each git push --force, that method is the recommended workflow.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

Problem: When I ran

git pull origin parent-branch

got the following error i.e. "Merge with strategy ort failed"

Soln:

git stash

git pull origin parent-branch

git apply stash 0

It started working.

Ziaullhaq Savanur
  • 1,848
  • 2
  • 17
  • 20