0

I have following situation:

There is a repo with name "MASTER". Fork "MASTER" to create "FORK_MASTER".

Suppose there are 10 commits pushed on "MASTER" and there are some two commits pushed on "FORK_MASTER".

Now I want to update FORK. How to achieve it?

My attempt to do same. I did following steps for achieving this: 1) Set up upstream for "FORK_MASTER" to "MASTER". 2) git fetch upstream 3) git rebase upstream/develop 4) git push origin

But git throws conflict after step 3 i.e. git rebase upstream/develop.

Looking forward to hear on how to achieve this.

Abhishek
  • 91
  • 2
  • 12
  • 1
    You have to [resolve the conflict](https://help.github.com/articles/resolving-merge-conflicts-after-a-git-rebase/), finish the rebase (there may be several more conflicts to resolve before your finished) and then you can push the result upstream to MASTER. – JDB Jan 17 '18 at 22:20
  • Possible duplicate of [git rebase merge conflict](https://stackoverflow.com/questions/11709885/git-rebase-merge-conflict) – JDB Jan 17 '18 at 22:22

2 Answers2

1

Since MASTER is the main repo (FORK_MASTER make contributions for the MASTER repo), you should pull the changes from MASTER repo first, and then rebase the changes from FORK_MASTER on the top.

Assume the local repo develop branch commit history as below:

…---A---B  develop

For MASTER repo, there are 10 commits C1 to C10 pushed. And for FORK_MASTER repo, there are 2 commits D1 and D2 pushed.

The usually workflow is pushing changes to FORK_MASTER and then create pull request to merge FORK_MASTER/develop into MASTER/develop (since the person works on the FORK_MASTER repo usually is not the administrator of the MASTER repo):

# In local FORK_MASTER repo
git remote add upstream https://github.com/user/MASTER -f
git checkout develop
git pull origin develop
git pull upstream develop --rebase
git push -f origin develop

The commit history will be:

…---A---B---C1---C2---…---C10---D1---D2 develop, origin/develop
                           |
                     upstream/develop  

Now the FORK_MASTER repo is updated. You can also update MSTAER repo by creating pull request to merge FORK_MASTER/develop into MASTER/develop. Or if you have permission to push changes to MASTER repo, you can push to MASTER repo directly by git push upstream master.

Note: when rebase commits D1 and D2 on the top of upstream/develop, if there has conflicts, you can modify and save the conflict files manually, and then use git add . and git rebase --continue.

Marina Liu
  • 36,876
  • 5
  • 61
  • 74
  • Still getting CONFLICT errors and below is log: Auto-merging CONFLICT (add/add): Merge conflict in – Abhishek Jan 18 '18 at 06:37
  • It's caused by fork repo and original repo changed the same file(s), and the conflicts can not be avoid. You can resolve the conflict manually or automatically. **Option 1**: resolve conflicts manually. As I list in the end of my answer, modeify the confilct file(s), then use `git add .` and `git rebase --continue`. **Option 2**: resolve conflicts automatically. You should add `-X ours/theirs` for the command `git pull upstream develop --rebase`. If you are in the rebasing process, abort it by `git rebase --abort`, and then execute the command `git pull upstream develop --rebase -X theirs`. – Marina Liu Jan 18 '18 at 06:50
  • 1
    The command `git pull upstream develop --rebase -X theirs` will resolve the conflicts automatically by keeping the fork repo's version. If you want to auto resolve the conflict file(s) by keeping the version from original repo, you can use the command `git pull upstream develop --rebase -X ours` instead. – Marina Liu Jan 18 '18 at 06:54
  • git pull upstream develop --rebase -X ours : This keeps version of file from master git pull upstream develop --rebase -X theirs: This keeps version of file from fork_master – Abhishek Jan 18 '18 at 06:57
  • Yes, they are. Exactly for rebase. – Marina Liu Jan 18 '18 at 06:59
  • with -X ours, getting following error: Your branch and 'origin/develop' have diverged, and have 1 and 1 different commits each, respectively. (use "git pull" to merge the remote branch into yours) nothing to commit, working tree clean – Abhishek Jan 18 '18 at 14:40
  • Since it's rebase the commits from FORK_MASTER on the top of the commits on MASTER repo, the origin/develop will be diverged. So you should use add `-f` (`--force`) option in `git push` command, as the last command in my answer. – Marina Liu Jan 19 '18 at 01:15
0

I have solved this problem using following command:

git remote -v (To check origin repo location)

git remote add upstream (To add upstream repo path)

git fetch upstream (To fetch branch from upstream repo)

git merge upstream/ --allow-unrelated-histories -X ours -m "Updating branch" (To merge changes from upstream branch to forked branch)

git push -f origin --all (Pushing changes to forked branch)

With these commands, I am successfully abled to update forked branch.

Abhishek
  • 91
  • 2
  • 12