git 我如何清理我的 Github 分支,以便我可以发出干净的拉取请求?

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

How do I clean up my Github fork so I can make clean pull requests?

gitgithub

提问by pjmorse

I forked a repository on Github. I've made some minor changes and submitted pull requests to the upstream, but along the way my fork has become so mangled I'm unable to generate clean pull requests; when I start a pull request from a branch with six changes, Github wants to submit thirteen, with seven of them already existing upstream (natch).

我在 Github 上创建了一个仓库。我做了一些小改动并向上游提交了拉取请求,但一路上我的叉子变得如此混乱,我无法生成干净的拉取请求;当我从具有六个更改的分支开始拉取请求时,Github 想要提交 13 个,其中 7 个已经存在于上游(natch)。

My problem seems to be related to only pulling the latest commits, but when I create a new branch and cherry-pick commits I still have extras. I've futzed with rebasingas well, but now it looks like even my masteris so messed up I can't generate a clean copy of upstream. This is apparently because I didn't understand that I needed to rebase instead of merging, so clearly I've made mistakes; what I'm trying to do is figure out how to unsnarl that knot and get back to a clean state where I can move forward usefully.

我的问题似乎拉取最新提交有关,但是当我创建一个新分支并挑选提交时,我仍然有额外的提交。我futzed与基础重建为好,但现在看起来,即使我的主人是那么糟糕,我不能生成的原始副本上游。这显然是因为我不明白我需要rebase 而不是 merging,所以很明显我犯了错误;我想要做的是弄清楚如何解开那个结并回到一个干净的状态,在那里我可以有用地前进。

I kind of want to blow away my fork and make a new fork of the upstream, but I gather that, too, is difficult.

我有点想吹掉我的叉子并在上游做一个新的叉子,但我认为这也很困难。

Having confessed my Git sins, how do I obtain github absolution?

承认了我的 Git 罪过,我如何获得 github 赦免?

回答by Bob Fanger

Step 1: Pull upstream changes
It's recommended to add the upstream repo as "upstream" as explained on the Fork a Repopage:

第 1 步:拉取上游更改
建议将上游存储库添加为“上游”,如Fork a Repo页面所述:

git pull --rebase upstream master

The --rebaseoption places your changes on top of the latest commit without merges.

--rebase选项将您的更改置于最新提交之上,无需合并。

Step 2: (Optional) Merge your commits into 1 commit

第 2 步:(可选)将您的提交合并为 1 个提交

git reset --soft upstream/master

This command will "undo" all your commits, but won't change the files. So you can commit all your changes in a single commit.

此命令将“撤消”您的所有提交,但不会更改文件。因此,您可以在一次提交中提交所有更改。

git commit -a

Step 3: Check & test your changes

第 3 步:检查并测试您的更改

To show the changes use a GUI like the built-in gitk, Sourcetree, TortoiseGitor Tower(paid), etc.

要显示更改,请使用 GUI 之类的内置gitkSourcetreeTortoiseGitTower (paid)等。

Step 4: Push

第 4 步:推

git pushwill throw an error, because the push would change the target repository's history.
If you're confident the changes shown in step 3 are correct then push with "-f"

git push将抛出错误,因为推送会更改目标存储库的历史记录。
如果您确信第 3 步中显示的更改是正确的,请使用“-f”推送

git push -f origin master


Additional information
The command to add a remote is:


附加信息
添加遥控器的命令是:

git remote add upstream git://github.com/[username]/[project].git

You can also also pull from a direct URL:

您还可以从直接 URL 中提取:

git pull --rebase  git://github.com/[username]/[project].git

But then you'll need the hash of the latest upstream commit instead of "upstream/master" in the other steps.

但是,在其他步骤中,您将需要最新的上游提交的哈希值,而不是“上游/主”。

回答by Sean Edwards

As I understand it, with both Git and Mercurial (I've only used the latter, so I may be wrong) it isn't a big deal at all to blow away a fork and re-fork it. I do that all the time with my projects. If you're ok with doing that (can back up your changes, or don't have any significant changes in your fork), I'd say that's probably the way to go.

据我了解,对于 Git 和 Mercurial(我只使用了后者,所以我可能是错的),吹掉一个叉子然后重新叉起来根本没什么大不了的。我在我的项目中一直这样做。如果您同意这样做(可以备份您的更改,或者您的 fork 中没有任何重大更改),我会说这可能是要走的路。

Remember, with DVCS, forking a repository makes a full clone of that entire repo. If you delete your current fork and then fork the original repo again, you'll have a completely clean slate to work from.

请记住,对于 DVCS,分叉存储库会生成整个 repo的完整克隆。如果您删除当前的 fork,然后再次 fork 原始 repo,您​​将有一个完全干净的工作。

回答by Seth Robertson

On your private repo, add the forkee's repo as a remote. Rebase/reset your branches from the remote's branches. Do a force push to your github repo.

在您的私有仓库中,将 forkee 的仓库添加为远程仓库。从远程分支重新设置/重置您的分支。强制推送到您的 github 存储库。

If you need exact commands, let me know. Also let me know whether you want to try and preserve local commits or if "blowing away" is OK.

如果您需要确切的命令,请告诉我。还让我知道您是否想尝试保留本地提交,或者“吹走”是否可以。