Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 920
user2462805 Avatar asked Nov 29 '16 11:11

user2462805


2 Answers

$ git checkout feature                  
$ git fetch
$ git pull origin master

Now conflicts should occur. Now if you want to keep master's changes

$ git status                           # See the untracked files (conflict files)
$ git checkout --theirs <file-name>    # Accept the master's change

Or, If you want to keep yours (features changes ):

$ git checkout --ours <file-name>

Add, commit and push in feature branch

$ git commit -am <commit-message>      # Add & Commit
$ git push origin HEAD                 # Push to remote feature branch
like image 78
Sajib Khan Avatar answered Sep 21 '22 13:09

Sajib Khan


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.

like image 32
Tim Biegeleisen Avatar answered Sep 17 '22 13:09

Tim Biegeleisen