34

我在 github 上创建了一个仓库。我做了一些更改并提出了拉取请求。

现在我做了一些其他的更改并想做一个新的拉取请求,但是在执行拉取请求之前的预览屏幕上,它也显示了旧的提交(那些已经被接受的提交)。

如何仅选择我的分叉存储库的主分支中的最新提交,以便我可以仅使用该提交进行拉取请求?

4

2 回答 2

47

一位同事的回答解决了我的问题:

git checkout -b NEW_BRANCH_NAME LAST_COMMIT_NAME_BEFORE_THE_ONE_WANTED
git cherry-pick COMMIT_NAME_WANTED
git push origin NEW_BRANCH_NAME

然后在 GitHub 上,您可以对您创建的新分支进行拉取请求。

更新

当我第一次开始使用 git 时,我问并回答了这个问题。现在我对它有了更多的了解,我想扩展这个答案。

使用 fork 时,您可能希望保持它相对于原始 repo 的更新。所以这些将是我今天要遵循的步骤:

git remote add upstream GIT_URL_OF_THE_ORIGINAL_REPO

现在你有一个upstream指向那个 repo 的引用。默认情况下,您还应该调用另一个origin在这种情况下指向您的 fork 的名称。这就是人们通常如何命名这些引用,但您可以使用任何您想要的名称upstreamorigin

现在您需要获取最新的更改:

git fetch upstream

然后,如果您想使用您的更改来更新您的分叉,upstream请执行以下操作:

git checkout master //checkout your master branch
git merge upstream/master //merge changes from the master branch in upstream into the branch you checked out (master)
git push origin master //if you want to push your updated master to your remote fork

Now, to answer the original question, what I would do today if I wanted to submit a new PR, would be:

git fetch upstream //get the latest changes from the original repo
git checkout -b my_new_feature upstream/master //create a new branch, starting from the master in the original repo
git cherry-pick WHATEVER_COMMIT_I_WANT //select the commit I want and add it to this new branch
git push origin my_new_feature //push a new branch to my fork

Then I would request a new PR for the my_new_feature branch.

You can replace git cherry-pick WHATEVER_COMMIT_I_WANT with just modifying/adding a file and then doing git add FILENAME, git commit -m "Fixing some stuff".

于 2011-04-06T21:18:49.070 回答
4

您可能会将提交放在单独的分支中(在第一组提交之前分支),假设它们与第一次提交无关。然后,单独的分支可以成为拉取请求的目标,并且只包含您的第二组提交。

于 2011-04-06T05:36:34.177 回答