我如何清理我的 Github 分叉,以便我可以做干净的拉请求?

我在 Github 上建了个仓库。我做了一些小改动,并向上游提交了 pull 请求,但是在这个过程中,我的 fork 变得非常混乱,我无法生成干净的 pull 请求; 当我从一个分支启动一个 pull 请求时,Github 需要提交13个,其中7个已经存在于上游(当然)。

我的问题似乎与 只提取最新的提交有关,但是当我创建一个新的分支和初选提交时,我仍然有额外的问题。我也用过 重新定位,但是现在看起来就连 我的主人都搞砸了,我不能生成一个干净的 逆流而上拷贝。这显然是因为我不明白我需要 而不是合并,所以很明显我犯了错误; 我试图做的是找出如何解开这个结,回到一个干净的状态,在那里我可以有效地前进。

我有点想吹走我的叉子,在上游建一个新的叉子,但我想,这也是很困难的。

在忏悔了我的 Git 罪恶之后,我如何获得 github 的赦免?

22572 次浏览

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.

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.

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.

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.

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

git pull --rebase upstream master

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

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

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

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

Step 4: Push

git push will 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 -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:

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.