Resolving pull requests with merge conflicts when using branch permissions in Bitbucket Server
On BitBucket server, when we get any conflict while merging any pull request, we can use git bash tool to resolve it on our local system and then we can commit and push our changes to remote feature branch and merge it to main branch.
Following steps need to be followed in order in our local system’s git bash tool.
(1) Open git bash tool and checkout or switch to your local feature branch.
(2) Pull the latest changes from the main branch (say 'master') into feature branch.
git pull origin master
(3) If above command fails due to some local changes then use below command to stash them otherwise move to next step.
git stash
followed by -
git pull origin master
(4) In case of conflict, automatic merge will fail so we need to merge it manually. Use below command to resolve conflicts.
git mergetool
By default, it will display all the available merge tools and one of them will be picked automatically. If we feel we are much comfortable with any other tool then we can also configure that and git will open that tool for us for conflict resolution.
(5) Once the conflicts are resolved then commit the changes into feature branch.
git commit
(6) Push the changes to remote feature branch.
git push
Verify on BitBucket server, now pull request should get updated automatically.
Again try to merge it; in case of no conflict it will get merged successfully.
If it has merge conflict again (if someone has committed new changes in main branch during we were resolving conflict on our local system) then follow the above steps again to resolve them.
We should be able to successfully resolve any conflicts if we have followed the above steps in order.
Thanks, hope it helps.
When we get into the dirty automatic merge scenario, whether by branch permissions or by automerge conflicts, using feature/bugfix branches, we do the following:
In your local clone of the repository:
- Checkout the destination branch with 'git checkout [destination branch]'.
- Update the master branch with the most recent changes from the remote with 'git pull origin [destination branch on remote]'.
- Checkout a new feature branch with 'git checkout -b feature/[my branch description]'
- Get the latest changes from the source branch with 'git fetch origin [source branch]'.
- Merge the fetched changes into the destination branch with 'git merge FETCH_HEAD'. We used 'git fetch' instead of 'git pull' to avoid checking out the source branch and updating it first. Thus the difference between 'git merge [source branch]' and 'git merge FETCH_HEAD' (after 'git fetch origin [source branch]') is that the latter will merge the state of that branch on the remote, while the former merges the local state of that branch (which may not be the same or might not even exist).
- Now fix the conflicts.
- Stage the changed files with 'git add' or 'git stage'.
- Commit the changes to the feature branch with 'git commit -m [your commit message]'.
- Push the changes to the remote with 'git push origin feature/[my branch description]'.
- Start a new pull request in Bitbucket with the URL provided in the response message. Make sure to select the correct destination branches. Auto-merging should be allowed to continue when possible.
- Decline the original pull request now that you have created the new one. Ensure no other commits were added to the original PR automatically in your absence.
Here it is on the CLI:
git checkout <destination branch>
git pull origin <destination branch on remote>
git checkout -b feature/<my branch description>
git fetch origin <source branch>
git merge FETCH_HEAD
<fix conflicts>
git stage <changed files>
git commit -m <my message>
git push origin feature/<my branch description>
<continue in bitbucket>
The instructions, unfortunately, come a bit unstuck with certain combinations of permssions, and it's something we hope to fix sometime (I work on Bitbucket).
To get around the issue you can resolve the conflicts on the source branch by merging changes in from the target branch.