Git - Fixing conflict between master and feature branch before pull request

user2462805 picture user2462805 · Nov 29, 2016 · Viewed 14.7k times · Source

I have to make a pull request to a repo. I clone the project and it has only one branch : master

I checkout a new branch feature and commit my changes to this branch.

I push my branch to Github and I'm now able to make a pull request. So I do it but it tells me now that I have conflicts. enter image description here

I understand the problem. master is ahead of feature because I didn't "pull" the last changes that have been made to master when I waas working on feature.

How can I fix it ? How can I "pull" the last changes of master into my feature branch ?

Answer

Tim Biegeleisen picture Tim Biegeleisen · Nov 29, 2016

Fixing via merge:

git fetch origin         # gets latest changes made to master
git checkout feature     # switch to your feature branch
git merge master         # merge with master
# resolve any merge conflicts here
git push origin feature  # push branch and update the pull request

Fixing via rebase:

git fetch origin         # gets latest changes made to master
git checkout feature     # switch to your feature branch
git rebase master        # rebase your branch on master
# complete the rebase, fix merge conflicts, etc.
git push --force origin feature

Note carefully that the --force option on the push after rebasing is probably required as the rebase effectively rewrites the feature branch. This means that you need to overwrite your old branch in GitHub by force pushing.

Regardless of whether you do a merge or a rebase, afterwards the conflicts should be resolved and your reviewer should be able to complete the pull request.