git 在 GitHub 中的分叉之间合并

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1123344/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:43:42  来源:igfitidea点击:

Merging between forks in GitHub

gitgithub

提问by R. Martinho Fernandes

I forked a GitHub repository. Then I pushed some changes to my fork. Then the original repository merged my changes and some others. Now, I want to merge those changes I'm missing. I tried a simple pull followed by push, but this yield my commits in duplicate. What's the best way to do it?

我分叉了一个 GitHub 存储库。然后我对我的叉子进行了一些更改。然后原始存储库合并了我的更改和其他一些更改。现在,我想合并我遗漏的那些更改。我尝试了一个简单的 pull 和 push,但这产生了我的重复提交。最好的方法是什么?

回答by Jim Puls

You probably have a "remote" for each repository. You need to pull from the one remote and push to the other.

您可能对每个存储库都有一个“远程”。您需要从一个遥控器拉动并推到另一个遥控器。

If you originally cloned from your fork, that remote will be called "origin". If you haven't added it already, you'll need to add the first person's repository as another remote:

如果您最初是从 fork 克隆的,那么该遥控器将被称为“原点”。如果您还没有添加它,则需要将第一个人的存储库添加为另一个远程:

git remote add firstrepo git://github.com/first/repo.git

After that's all set up, you should indeed be able to

完成所有设置后,您确实应该可以

git pull firstrepo master
git push origin

Remember, git pullis nothing more than a macro that does git fetchand git merge, in that order. You just need to fetch the list of commits from the first person's repository and then merge their branch into your tree. Merging should do the right thing with your commits on both branches.

请记住,git pull无非是一个按顺序执行git fetch和的宏git merge。您只需要从第一个人的存储库中获取提交列表,然后将他们的分支合并到您的树中。合并应该对你在两个分支上的提交做正确的事情。

GitHub, in all its perpetual awesomeness, gives you a shortcut, of course. There's a "fast-forward" button on your fork of the repository that you can use to catch your fork up if you're entirely merged in to the other side.

当然,GitHub 以其永恒的魅力为您提供了一条捷径。如果您完全合并到另一侧,您的存储库分叉上有一个“快进”按钮,您可以使用它来赶上您的分叉。

回答by Bob Spryn

So the accepted answer above didn't work for me perfectly. Namely, it seemed to lose the link to the original github author when it worked, and then didn't seem to work anymore after that. I think the problem was that the answer left out the / between the remote name and the branch. So it would fetch a branch called master from the remote, but then not be able to do anything with it. Not really sure why.

所以上面接受的答案对我来说并不完美。也就是说,它在工作时似乎失去了原始 github 作者的链接,然后似乎不再工作了。我认为问题在于答案遗漏了远程名称和分支之间的 / 。所以它会从远程获取一个名为 master 的分支,但随后无法对它做任何事情。不太确定为什么。

Here's the way github recommends from their site.

这是github 从他们的网站推荐的方式。

Once you have cloned your forked repo, you do need to add a remote pointing to the original like the previous answer said. They like to call it upstream, but it doesn't matter.

克隆分叉存储库后,您确实需要像之前的答案所说的那样添加指向原始存储库的远程存储库。他们喜欢称其为上游,但这并不重要。

git remote add upstream git://github.com/octocat/Spoon-Knife.git

Then you fetch

然后你取

git fetch upstream

and you'll see the versions available for merging

你会看到可用于合并的版本

From git://github.com/octocat/Spoon-Knife.git
 * [new branch]      gh-pages   -> upstream/gh-pages
 * [new branch]      master     -> upstream/master

Then you just need to choose the branch you want to merge in. Mind you these aren't local branches, they are stored under remotes. But provided you don't have a local branch called upstream/master (which is allowed) you should be fine merging with the line below:

然后你只需要选择你想要合并的分支。请注意,这些不是本地分支,它们存储在遥控器下。但是如果您没有名为 upstream/master 的本地分支(这是允许的),您应该可以与下面的行合并:

git merge upstream/master

Alternatively you could shortcut the fetch/merge (after the initial fetch at least) with this line:

或者,您可以使用以下行快捷方式提取/合并(至少在初始提取之后):

git pull upstream/master