清理叉子并从上游重新启动它

时间:2012-03-10 11:48:22

标签: git

我已经分叉了一个存储库,然后我做了一些更改,看起来我搞砸了所有东西。

我希望从头开始,使用当前的上游/主人作为我工作的基础 我应该改装我的存储库还是删除它?

4 个答案:

答案 0 :(得分:678)

最简单的解决方案是(使用'upstream'作为引用原始仓库分叉的远程名称):

git remote add upstream /url/to/original/repo
git fetch upstream
git checkout master
git reset --hard upstream/master  
git push origin master --force 

(与此GitHub page, section "What should I do if I’m in a bad situation?"类似)

请注意,您可能会丢失对master分支所做的更改(由于reset --hard而在本地,并且在远程端,因为{{} 1}})。

如果您希望保留push --force上的提交,则可以选择在当前master之上重播这些提交。
upstream/master替换重置部分。然后你仍然需要强制推动 另请参阅“What should I do if I’m in a bad situation?


更完整的解决方案,备份您当前的工作(以防万一)详见“Cleanup git master branch and move some commit to new branch”。

另请参阅“Pull new updates from original GitHub repository into forked GitHub repository”以说明“git rebase upstream/master”是什么。

upstream


注意:最近的GitHub回购对upstream执行protect the master branch 因此,您必须先取消保护push --force(见下图),然后re-protect it after force-pushing)。

enter image description here


注意:特别是在GitHub上,有now (February 2019)快捷方式删除已经合并到上游的拉请求的分叉repos。

答案 1 :(得分:27)

爱VonC的答案。这是初学者的简单版本。

有一个名为origin的git遥控器,我相信你们都知道。基本上,您可以根据需要为git repo添加任意数量的遥控器。所以,我们能做的是引入一个新的遥控器,它是原始的回购而不是前叉。我喜欢称之为original

让我们将原始回购添加到我们的fork作为远程。

git remote add original https://git-repo/original/original.git

现在让我们获取原始回购以确保我们拥有最新的编码

git fetch original

正如VonC建议的那样,确保我们是主人。

git checkout master

现在为了让我们的叉子快速使用原始仓库上的最新代码,我们所要做的就是根据原始遥控器硬重置我们的主分支。

git reset --hard original/master

你完成了:)

答案 2 :(得分:4)

如何通过Sourcetree GUI 100%做到这一点

(并非每个人都喜欢通过git命令行界面进行操作)

  

设置完成后,您只需要从此开始执行步骤7-13。

     

获取>检出master分支>重置为其主master>将更改推送到服务器

步骤

  1. 在屏幕顶部的菜单工具栏中:“存储库”>“存储库设置”

"Repository" highlighted in the top menu bar

  1. “添加”

"Add" button at the bottom of the dialog

  1. 返回GitHub并复制克隆URL。

"Clone or Download" button on the Github website followed by the git url

  1. 将网址粘贴到“网址/路径”字段中,然后为其指定一个有意义的名称。我称它为“大师”。 请勿选中“默认远程”复选框。您将无法直接推送到该存储库。

"Remote name" and "URL / Path" fields highlighted in the"Remote details" dialog

  1. 按“确定”,您应该立即看到它出现在您的存储库列表中。

"master" repository added to the list of repositories in the "Repository settings" dialog

  1. 再次按“确定”,您应该会看到它出现在“远程”列表中。

"master" repository highlighted in remotes list in side bar

  1. 单击“获取”按钮(在“源”树标题区域的左上方)

"Fetch" button in the header area

  1. 确保已选中“从所有远程获取”复选框,然后按“确定”

"Fetch from all remotes" checkbox highlighted in the "Fetch" dialog

  1. 双击“ master”分支以检查是否尚未签出。

  2. 找到要重置为的提交,如果您将存储库称为“ master”,则很可能希望找到带有“ master / master”标签的提交。

Example of a commit with a "master/master" tag on it

  1. 右键单击commit>“将当前分支重置为此提交”。

  2. 在对话框中,将“使用模式:”字段设置为“硬-放弃所有工作副本更改”,然后按“确定”(确保将您不想丢失的所有更改都放到首先是单独的分支)。

"Using mode" field highlighted in the "Reset to commit" dialog. It is set to "discard all working copy changes"

  1. 单击“推”按钮(在“源”树标题区域的左上方),将更改上传到您的存储库副本中。

"Push" button in the header area

您完成了!

答案 3 :(得分:3)

关注@VonC很棒的回答。您的GitHub公司政策可能不允许强行推送'在主人。

<VirtualHost *:80> servername prod.LOCALSERVERNAME DocumentRoot "E:/Production/Apache/htdocs" <Directory "E:/Production/Apache/htdocs"> Options +FollowSymLinks AllowOverride All </Directory> JkOptions +ForwardURICompatUnparsed ProxyRequests off JkMount /* worker1 </VirtualHost> <VirtualHost *:80> servername dummy.LOCALSERVERNAME DocumentRoot "E:/Production/Apache/dummy" JkOptions +ForwardURICompatUnparsed ProxyRequests off JkMount /* worker2 </VirtualHost>

如果收到类似此错误消息,请尝试以下步骤。

要有效地重置您的分叉,您需要按照以下步骤操作:

remote: error: GH003: Sorry, force-pushing to master is not allowed.

在GitHub上打开你的分叉,在&#34;设置 - &gt;分支 - &gt;默认分支&#34;选择&#39; new_master&#39;作为新的默认分支。现在你可以强行推动'#master;&#39;分支:

git checkout master
git reset --hard upstream/master
git checkout -b tmp_master
git push origin

然后你必须退回'#master;&#39;作为GitHub设置中的默认分支。要删除&#39; tmp_master&#39; :

git checkout master
git push --force origin

其他有关丢失更改的警告仍然适用,请保持警惕。