Git:如何从一个分支中挑选提交并为另一个分支创建拉取请求?

21

我有两个分支devmaster。我正在使用BitBucket。

我需要从dev分支中挑选一些提交内容。

然后需要为master分支创建拉取请求。

由于我的dev环境有很多东西,其中一些无法直接合并到master中。

因此,从dev分支获取提交 <id 1>、<id 2>,将它们一起获取,创建一个拉取请求,将它们与master分支合并。


使用GIT可以让您挑选特定的提交记录,通过提交ID,而不管分支如何。 例如:git cherry-pick <id 1> - MaD
你可以在主分支上创建一个新的分支。挑选所需的提交,然后为这个新分支创建一个PR。在你的情况下,它将包含你挑选的提交。 - MaD
没有合并涉及。Cherry-pick会选择所需的提交,并将它们放在您的主分支之上。然后,您可以将这些提交推送到新分支并创建PR。 - MaD
你能提供完整的步骤清单吗? - Mighty
让我们在聊天中继续这个讨论 - Mighty
显示剩余2条评论
2个回答

34

您可以使用

git cherry-pick <commit id>

选择特定的提交

为了完成周期并从主分支创建PR。 我们可以执行以下步骤:

假设当前在master分支上:

git checkout -b myNewBranch // this will create a new branch named myNewBranch
git cherry-pick <commitID 1> // this will take the commit with the commit ID 1 and 
                             // attempt to place it on top of the master branch. 
                             // Note however, there might be conflicts to resolve
git cherry-pick <commitID 2> // this will take the commit with the commit ID 2 and place on top of the master branch
git push origin/<some branch name> // will push the changes to remote. Usually origin/<local branch name>

然后根据您的平台,您可以创建一个拉取请求。可以通过GUI在GitHub平台或DevAzure等平台上进行操作,也可以通过BitBucket GUI在您的情况下进行操作。

顺便提一句:上述步骤是为了简单起见。也可以使用一行代码来进行cherry-pick。就像这样:

git cherry-pick <commitID 1> <commitID 2>

我们需要从dev分支中挑选提交。因此,将从dev创建新分支。然后从中挑选提交。然后通过PR将此新分支合并到主分支。正确吗? - Mighty
如果你需要从dev选择提交到master,则应在master上创建新分支。然后从dev中挑选提交。接下来将分支推送到远程,并使用Bitbucket GUI程序进行PR。如答案所述。如果需要进一步协助(关于合并),我相信你可以在StackOverflow上找到答案,或者如果没有找到合适的答案,可以提出另一个问题。 - MaD
如果答案解决了您的问题,请将其标记为正确答案。 - MaD

6
Fork original-repo as "your-repo"

git clone your-repo
cd your-repo
git checkout dev
git pull //to get all your commits to local
git checkout master
git pull //to make sure branch is upto date
git cherry-pick commit-id
git push //commits the cherry-picked commits to the master branch of forked repo

Raise PR from Forked repo "your-repo" master branch to "original-repo" master branch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接