Thursday, October 25, 2018

Git: Resolve Conflicts in PR

What to do when GitHub does not allows you to merge a Pull Request due to conflicts!
First of all, rebase often to avoid this problem in most cases.
Sharing bits from my experience. The solutions that worked for me are:

1. git rebase

The best way is to do it locally using terminal or command prompt. Lets say you raised a PR from origin/develop to upstream/develop branch and got conflict. Go to your command prompt, rebase master onto develop and push to develop branch. PR should get resolved.
But this won’t work when the tips are just big time outdated.
  • I tried rebasing, and all I ended up was at same state but with alternated branches. Means the new branch was now compatible with master but not develop.
  • I also tried using GitHub’s web editor (link was present in PR comment near conflicting files), this also did not work, as after resolving, GitHub repeatedly asked me to Start Over.
That’s when git merge came to rescue.


2. git merge

Assuming, you are raising PR from your origin to another remote called upstream, perform the following in your local terminal or command prompt.
git checkout develop 
git fetch upstream 
git merge upstream/develop
Resolve any conflicts and commit. The run below to check if branch is clean now.
git status
Once confirmed, execute.
git push origin develop
And then, raise PR from origin/develop to upstream/develop. You should be able to merge it now.

3. Force push

Use this option if and only if you are sole owner of upstream and are confident that you’ve got all commits in your origin/develop.

Force Push overwrites target branch with your pushed commits, and you won’t be able to reclaim whats lost in the process.
git push origin develop 
git checkout -b develop upstream/develop 
git reset --hard origin/develop 
git push upstream +develop

Note: origin and upstream are just regular remotes, former is your own remote repository and upstream represents the base repository from which you have forked your’s. For example,
upstream -> https://github.com/microsoft/msoffice 
origin -> https://github.com/rajdeep/msoffice
Thats it! I hope it helps. Let me know in comments if this worked or you have some other scenario or solution.

No comments:

Post a Comment

Liked or hated the post? Leave your words of wisdom! Thank you :)