How to update forked repo with original repo if both forked repo and original repo has some commits pushed?

青春壹個敷衍的年華 提交于 2019-12-13 03:54:53

问题


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.


回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/48307297/how-to-update-forked-repo-with-original-repo-if-both-forked-repo-and-original-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!