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

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

Clean up a fork and restart it from the upstream

git

提问by tampe125

I have forked a repository, then I made some changes and it looks like I've messed up everything.

我已经分叉了一个存储库,然后我做了一些更改,看起来我把一切都搞砸了。

I wish to start it again from scratch, using the current upstream/master as the base for my work.
Should I rebase my repository or delete it at all?

我希望从头开始,使用当前的上游/主作对我来说有效的基础。
我应该重新设置我的存储库还是完全删除它?

回答by VonC

The simplest solution would be (using 'upstream' as the remote name referencing the original repo forked):

最简单的解决方案是(使用“ 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 

(Similar to this GitHub page, section "What should I do if I'm in a bad situation?")

(类似于这个GitHub 页面,“如果我处于糟糕的情况怎么办?”部分

Be aware that you can lose changes done on the masterbranch(both locally, because of the reset --hard, and on the remote side, because of the push --force).

请注意,您可能会丢失在master分支上所做的更改(无论是在本地,因为reset --hard,还是在远程端,因为push --force)。

An alternative would be, if you want to preserve your commits on master, to replay those commits on top of the current upstream/master.
Replace the reset part by a git rebase upstream/master. You will then still need to force push.
See also "What should I do if I'm in a bad situation?"

另一种选择是,如果您想保留对 的提交master,则在当前upstream/master.
用 替换重置部分git rebase upstream/master。然后你仍然需要强制推送。
另请参阅“如果我处于糟糕的境地,我该怎么办?



A more complete solution, backing up your current work (just in case) is detailed in "Cleanup git master branch and move some commit to new branch".

一个更完整的解决方案,备份您当前的工作(以防万一)在“清理 git master 分支并将一些提交移动到新分支”中详细说明。

See also "Pull new updates from original GitHub repository into forked GitHub repository" for illustrating what "upstream" is.

另请参阅“将新更新从原始 GitHub 存储库拉入分叉的 GitHub 存储库”以说明“ upstream”是什么。

upstream

上游的



Note: recent GitHub repos do protect the masterbranchagainst push --force.
So you will have to un-protect masterfirst (see picture below), and then re-protect it after force-pushing).

注意:最近的 GitHub 存储库确实可以保护master分支免受push --force.
所以你必须先取消保护master(见下图),然后在强制推动后重新保护它)。

enter image description here

在此处输入图片说明



Note: on GitHub specifically, there is now (February 2019)a shortcut to delete forked repos for pull requests that have been merged upstream.

注意:特别是在 GitHub 上,现在(2019 年 2 月)有一个快捷方式可以删除已合并到上游的拉取请求的分叉存储库。

回答by Ahmad Awais

Love VonC's answer. Here's an easy version of it for beginners.

喜欢 VonC 的回答。这是适合初学者的简单版本。

There is a git remote called originwhich I am sure you are all aware of. Basically, you can add as many remotes to a git repo as you want. So, what we can do is introduce a new remote which is the original repo not the fork. I like to call it original

有一个叫做 git remote 的东西origin,我相信你们都知道。基本上,您可以根据需要向 git 存储库添加任意数量的遥控器。所以,我们可以做的是引入一个新的远程仓库,它是原始仓库而不是分叉。我喜欢叫它original

Let's add original repo's to our fork as a remote.

让我们将原始存储库作为远程添加到我们的叉子中。

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

Now let's fetch the original repo to make sure we have the latest coded

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

git fetch original

As, VonC suggested, make sure we are on the master.

正如 VonC 建议的那样,确保我们在主服务器上。

git checkout master

Now to bring our fork up to speed with the latest code on original repo, all we have to do is hard reset our master branch in accordance with the original remote.

现在为了让我们的分支跟上原始 repo 上的最新代码,我们所要做的就是根据原始远程硬重置我们的主分支。

git reset --hard original/master

And you are done :)

你完成了:)

回答by Hugodby

Following @VonC great answer. Your GitHub company policy might not allow 'force push' on master.

关注@VonC 很好的答案。您的 GitHub 公司政策可能不允许对 master 进行“强制推送”。

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

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

If you get an error message like this one please try the following steps.

如果您收到这样的错误消息,请尝试以下步骤。

To effectively reset your fork you need to follow these steps :

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

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

Open your fork on GitHub, in "Settings -> Branches -> Default branch" choose 'new_master' as the new default branch. Now you can force push on the 'master' branch :

在 GitHub 上打开您的分支,在“设置 -> 分支 -> 默认分支”中选择“new_master”作为新的默认分支。现在您可以强制推送“master”分支:

git checkout master
git push --force origin

Then you must set back 'master' as the default branch in the GitHub settings. To delete 'tmp_master' :

然后,您必须在 GitHub 设置中将“master”重新设置为默认分支。删除 'tmp_master' :

git push origin --delete tmp_master
git branch -D tmp_master

Other answers warning about lossing your change still apply, be carreful.

关于丢失更改的其他警告仍然适用,请小心。

回答by Daniel Tonon

How to do it 100% through the Sourcetree GUI

如何通过Sourcetree GUI100% 做到这一点

(Not everyone likes doing things through the git command line interface)

(不是每个人都喜欢通过 git 命令行界面做事)

Once this has been set up, you only need to do steps 7-13 from then on.

Fetch > checkout master branch > reset to their master > Push changes to server

设置完成后,从那时起您只需要执行步骤 7-13。

获取 > 结帐主分支 > 重置为他们的主分支 > 将更改推送到服务器

Steps

脚步

  1. In the menu toolbar at the top of the screen: "Repository" > "Repository settings"
  1. 在屏幕顶部的菜单工具栏中:“存储库”>“存储库设置”

"Repository" highlighted in the top menu bar

顶部菜单栏中突出显示的“存储库”

  1. "Add"
  1. “添加”

"Add" button at the bottom of the dialog

对话框底部的“添加”按钮

  1. Go back to GitHub and copy the clone URL.
  1. 返回 GitHub 并复制克隆 URL。

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

Github 网站上的“克隆或下载”按钮后跟 git url

  1. Paste the url into the "URL / Path" field then give it a name that makes sense. I called it "master". Do not check the "Default remote" checkbox. You will not be able to push directly to this repository.
  1. 将 url 粘贴到“URL / 路径”字段中,然后为其指定一个有意义的名称。我称它为“大师”。不要选中“默认远程”复选框。您将无法直接推送到此存储库。

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

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

  1. Press "OK" and you should see it appear in your list of repositories now.
  1. 按“确定”,您现在应该会看到它出现在您的存储库列表中。

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

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

  1. Press "OK" again and you should see it appear in your list of "Remotes".
  1. 再次按“确定”,您应该会看到它出现在您的“遥控器”列表中。

"master" repository highlighted in remotes list in side bar

"master" repository highlighted in remotes list in side bar

  1. Click the "Fetch" button (top left of the Source tree header area)
  1. 单击“获取”按钮(源树标题区域的左上角)

"Fetch" button in the header area

"Fetch" button in the header area

  1. Make sure the "Fetch from all remotes" checkbox is checked and press "ok"
  1. 确保选中“从所有遥控器获取”复选框,然后按“确定”

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

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

  1. Double click on your "master" branch to check it out if it is not checked out already.

  2. Find the commit that you want to reset to, if you called the repo "master" you will most likely want to find the commit with the "master/master" tag on it.

  1. 双击您的“主”分支以检查它是否尚未检出。

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

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

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

  1. Right click on the commit > "Reset current branch to this commit".

  2. In the dialog, set the "Using mode:" field to "Hard - discard all working copy changes" then press "OK" (make sure to put any changes that you don't want to lose onto a separate branch first).

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

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

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

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

  1. Click the "Push" button (top left of the Source tree header area) to upload the changes to your copy of the repo.
  1. 单击“推送”按钮(源树标题区域的左上角)将更改上传到您的存储库副本。

"Push" button in the header area

"Push" button in the header area

Your Done!

你完成了!