20

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 ?

user2462805
  • 1,049
  • 3
  • 11
  • 26

2 Answers2

38

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Hey, jumping on here. Same case, but after either fix I end up with a feature branch with commits from other branches that weren't previously in its history. Any insight as to why that may be? – jared Apr 10 '23 at 16:39
8
$ 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
Sajib Khan
  • 22,878
  • 9
  • 63
  • 73