git 如何将分叉仓库的分支合并到原始仓库的主分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14587045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to merge branch of forked repo into master branch of original repo?
提问by pemistahl
There is an open source project on GitHub. Its original repo contains the most recent master branch. Now there is someone (not me) who has forked this repo, created a development branch from the master branch and added stuff to the development branch. What I would like to do now is to merge the development branch back to the master branch. Unfortunately, the master branch of the forked repo is not up-to-date. Also note that I'm not in possession of the two respective remote repos.
GitHub 上有一个开源项目。它的原始 repo 包含最新的 master 分支。现在有人(不是我)分叉了这个 repo,从主分支创建了一个开发分支,并向开发分支添加了东西。我现在想做的是将开发分支合并回主分支。不幸的是,分叉回购的主分支不是最新的。另请注意,我不拥有两个各自的远程存储库。
At the moment, I have cloned both the original repo and the forked repo to my local machine using git clone
. How can I merge the development branch of the forked repo into the master branch of the original repo on my local machine (i.e. not on the remote server which is not possible anyway)?
目前,我已经使用git clone
. 如何将分叉存储库的开发分支合并到我本地机器上原始存储库的主分支中(即不在远程服务器上,这是不可能的)?
回答by helmbert
When using GitHub, you could do this using a Pull Request. Simply push your development branch to the forked remote repository and create the pull request as described in the linked article. The owner of the original repository can then add your repository as a new remote repository, fetch your changes and merge your development branch back into the master branch.
使用 GitHub 时,您可以使用Pull Request来做到这一点。只需将您的开发分支推送到分叉的远程存储库,并按照链接文章中的描述创建拉取请求。然后,原始存储库的所有者可以将您的存储库添加为新的远程存储库,获取您的更改并将您的开发分支合并回主分支。
It is usually not necessary for your master branch to be fully up-to-date -- although it is extremly helpful, especially when merging the development branch.
你的主分支通常没有必要完全更新——尽管它非常有用,尤其是在合并开发分支时。
First, you should bring the master
branch in the forked repository up-to-date:
首先,您应该将master
分叉存储库中的分支更新为最新:
$ git remote add upstream ssh://path/to/original/repo
$ git checkout master
$ git fetch upstream # Instead of "fetch" & "merge", "pull" would also work
$ git merge upstream/master
Then either rebase
or merge
your development branch into the master branch:
然后rebase
或者merge
你的开发分支进入主分支:
$ git checkout development
$ git rebase master # OR "git merge master"
Resolve any conflicts, if necessary. When merging, use git commit
to finish the merge, when rebasing use git commit && git rebase --continue
.
如有必要,解决任何冲突。合并时使用git commit
来完成合并,rebase 时使用git commit && git rebase --continue
.
You should now be able to git push
to the origin of the forked repository, and create a pull request. Alternatively, if you're authorized to do so, you can also push directly to the original repository from your fork.
您现在应该能够访问git push
分叉存储库的来源,并创建拉取请求。或者,如果您被授权这样做,您也可以直接从您的 fork 推送到原始存储库。
回答by Borealid
It sounds like you've already cloned both repositories. I'm going to assume you have them in directories clone1
and clone2
.
听起来您已经克隆了两个存储库。我将假设您将它们放在目录clone1
和clone2
.
You need to get all the changesets into one repository before you can do the merge. So we're going to pull from one of your clones into the other one. Were you doing this from scratch, you would just clone onerepository and then fetch the other, instead of cloning both.
在进行合并之前,您需要将所有变更集放入一个存储库中。所以我们将从你的一个克隆中拉到另一个。如果您从头开始执行此操作,您只需克隆一个存储库,然后获取另一个存储库,而不是同时克隆这两个存储库。
cd clone1
git checkout master
git remote add clone2 ../clone2
(put both remotes into one repo). If you hadn't cloned both repositories, you wouldn't have to do this - you'd just dogit remote add otherthing https://<github URI>
git fetch
(fetch changes fromclone2
intoclone1
). Now you have one repository with everything.git checkout origin/master
Or you can check out a local branch. Your preference. Remember,origin
here refers to the source forclone1
.git merge clone2/development
If necessary, resolve conflicts at this point. Commit the result.git checkout -b my-ongoing-development
(name the branch you just made, which contains both remote branches)
cd clone1
git checkout master
git remote add clone2 ../clone2
(将两个遥控器放入一个仓库)。如果你没有克隆两个存储库,你就不必这样做 - 你只需要git remote add otherthing https://<github URI>
git fetch
(从clone2
into获取更改clone1
)。现在您拥有了一个包含所有内容的存储库。git checkout origin/master
或者您可以查看当地的分支机构。你的偏好。请记住,origin
这里指的是clone1
.git merge clone2/development
如有必要,请在此时解决冲突。提交结果。git checkout -b my-ongoing-development
(命名您刚刚创建的分支,其中包含两个远程分支)