Git:如何向别人的分支提交代码?

4

我在一个开源项目中工作,并已经 fork 了它。有人也 fork 了它并正在使用一些有趣的想法,但不会很快推送到原始库。我想帮助他,因此我将他的 fork 克隆到了我的计算机上。然而,我不能直接将我的工作推送到他的 fork 上。我也不能再 fork 一次,因为我已经从原始库进行了 fork。我该怎么办?谢谢。


请返回翻译后的文本:https://dev59.com/Ol4c5IYBdhLWcg3wQoc5https://stackoverflow.com/search?q=%5Bgithub%5D+create+second+fork - phd
将分叉的存储库作为另一个远程存储库添加到本地存储库中,从该远程存储库获取更改,将要发送的工作重新设置到分叉的工作上,然后将更改推送到另一个远程存储库(或发送PR)。 - Jeff Mercado
@JeffMercado:你能添加一个包含所有必要的git命令的答案吗?谢谢。 - Tony
2个回答

3
假设您的代码库看起来像这样: enter image description here
#Add the forked repo as another remote to your local repository
git remote add someone https://fork.url                 #create remote called "someone"

#fetch the changes from that remote
git fetch someone

enter image description here

#rebase the work you want to send onto the fork's work
git branch for-someone my-branch                        #create branch "for-someone" at "my-branch"
git rebase master for-someone --onto someone/master     #take commits from the "for-someone" branch down to your "master" branch and rebase it onto someone's "master" branch

enter image description here

#then push your changes to the other remote
git push someone for-someone                            #push branch "for-someone" to the "someone" remote

请更改远程和分支名称为更合适的名称。


顺便说一句,如果你只想提交一个小东西/推送到PR/contributors fork/branch,你可以省略git remote add步骤,直接使用fork的URL(在这个例子中为https://fork.url)作为origin。也就是说,只需要执行:git push https://fork.url for-someone即可。这种方法在最近的git版本中也适用。 - rugk

0

你可以像@jeff-mercado提到的那样做两件事情。这将是一个复杂的任务,我建议你使用基于GUI的git客户端来完成。我曾经遇到过与你相同的问题,并发现Sublime Merge非常有用。

我建议你要求他向你发送PR。 然后你就能够将他的更改合并到你的存储库中 最后,你可以将合并后的存储库推送到你的github上,并要求他克隆并推送。 (这是假设另一个人不会做合并你的PR的工作,这对你来说会更容易。)


看起来你提到的情况并不是我当前所处的情况。有一个主仓库,他的分支和我(第三人称)想要为他的分支做出贡献,而不是主仓库或我的仓库。我想向他的仓库发起PR。 - Tony
这个链接可能会有所帮助 https://dev59.com/_m035IYBdhLWcg3wH8Ul - Anmol Deep
不,这并没有帮助,因为它只涉及到两个人,并且是从第二人合并到第一个人。 - Tony

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接