简体   繁体   English

从PR中已经存在的PR中排除提交

[英]Excluding a commit from a PR that is already in a PR

I have an open PR with a commit I did 我有提交的公开PR

I made another commit and want to create another PR with just these changes.. however the previous commit is in there. 我进行了另一次提交,只想通过这些更改来创建另一个PR。.但是,先前的提交在那里。

I realize branches would resolve this, yet, here we are. 我知道分支机构可以解决这个问题,但是,现在就到了。 Do I just need to wait for the PR to be merged? 我是否只需要等待PR合并?

I have an open PR with a commit I did 我有提交的公开PR

At that point your repository looked something like this. 那时,您的存储库看起来像这样。

A - B [master]
     \
      C [pr1]

Commit B is where master is at. 提交B是master所在的位置。 And you have a branch called pr1 with a commit C on it. 并且您有一个名为pr1的分支,上面带有提交C。

I made another commit and want to create another PR with just these changes.. however the previous commit is in there. 我进行了另一次提交,只想通过这些更改来创建另一个PR。.但是,先前的提交在那里。

What probably happened is you branched from pr1 rather than master resulting in this. 可能发生的情况是您从pr1而不是master分支了。

A - B [master]
     \
      C [pr1]
       \
        D [pr2]

When you make a PR for pr2 by default the base branch is master so it shows C and D in the PR. 默认情况下,为pr2创建PR时,基本分支为master因此它在PR中显示CD


If the work in D does not depend on C what you want is for pr2 and commit D to be branched from master . 如果D的工作不依赖于C那么您想要的是pr2并提交Dmaster分支。

      D [pr2]
     /
A - B [master]
     \
      C [pr1]

You can fix this by rebasing pr2 onto master . 您可以通过将pr2重新pr2master上来解决此问题。

git rebase --onto master pr1 pr2

That says to replay the commits starting at (but not including) pr1 to (and including) pr2 onto master . 那就是说将从pr1开始(但不包括)到pr2 (包括pr2 )的提交重播到master Then you can push pr2 and create an independent PR. 然后,您可以推送pr2并创建一个独立的PR。


If the work in D does depend on C then it's best to leave pr2 on top of pr1 . 如果D的工作确实取决于C那么最好将pr2放在pr1之上。 Create a PR for pr2 with the base being pr1 instead of master . pr2创建PR,其基础为pr1而不是master Once pr1 is reviewed and accepted, then pr2 can be reviewed and accepted. pr1被审核并接受后, pr2即可被审核并接受。

Be sure to make a note in the PR description that pr2 is stacked on top of pr1 so the reviewer doesn't miss it. 确保在PR描述中记下pr2堆叠在pr1顶部,这样审阅者就不会错过它。 "This depends on #123" where #123 is the ID of pr1 's PR. “这取决于#123”,其中#123是pr1的PR的ID。

Just create a new branch from whatever point you want to start and cherry-pick the other branch. 只需从要开始的任何点创建一个新分支,然后挑选其他分支即可。 That will apply the changes involved on the last revision of the other branch and no other changes (assuming it's the last revision from that branch). 这将应用涉及另一个分支的最新修订版的更改,而不会应用其他更改(假设它是该分支的最新修订版)。

An easy way to do this, is by using interactive rebase . 一种简单的方法是使用交互式rebase

If you run git log --oneline on your branch, you should see something that looks like this: 如果在分支上运行git log --oneline ,则应该看到类似以下内容的内容:


ccccccc (HEAD -> your-branch-name) Your (good) commit you want to keep
bbbbbbb bbbbbbb Your (bad) commit you want to remove from history
aaaaaaa (master) Commit from you or someone else

当前

You say you want to remove bbbbbbb replacing it with ccccccc . 您说要删除bbbbbbb将其替换为ccccccc

Because ccccccc contains changes since bbbbbbb , removing it would cause lots of conflicts, as you'd have to merge ccccccc into aaaaaaa . 由于cccccccbbbbbbb以来包含更改,因此删除它会引起很多冲突,因为您必须将ccccccc合并为aaaaaaa

The git way to do it, is to squash ccccccc into bbbbbbb , creating a new commit ddddddd . git方式做到这一点,是南瓜 cccccccbbbbbbb ,创建一个新的提交ddddddd And for that, you'd do the following steps: 为此,您需要执行以下步骤:

1 . 1 Run git rebase -i aaaaaaa to open the interactive rebase, starting from the commit immediatelly before the one you want to remove 运行git rebase -i aaaaaaa打开交互式rebase,从要删除的提交之前立即开始提交

This will open your configured text editor with something that looks like this: 这将打开您配置的文本编辑器,如下所示:


pick bbbbbbb Your (bad) commit you want to remove from history
pick ccccccc Your (good) commit you want to keep

# Rebase aaaaaaa..f5a9551 onto aaaaaaa (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
#   However, if you remove everything, the rebase will be aborted.
#
#   
# Note that empty commits are commented out

The list shows what the interactive rebase will do: From aaaaaaa , pick bbbbbbb , then pick ccccccc 该列表显示了交互式rebase的作用:从aaaaaaa ,选择bbbbbbb ,然后选择ccccccc

2 . 2 As I said above, you want to squash ccccccc into bbbbbbb , so replace the word pick before ccccccc by squash : 正如我前面所说,你要挤进 cccccccbbbbbbb ,所以替换词pickcccccccsquash


pick bbbbbbb Your (bad) commit you want to remove from history
squash ccccccc Your (good) commit you want to keep

3 . 3 Save the file, and close the text editor. 保存文件,然后关闭文本编辑器。

4 . 4 Another text editor will open, to ask you to specify the message of the new commit being created, with the combination of ccccccc and bbbbbbb - which I'm calling ddddddd for the sake of this example: 将打开另一个文本编辑器,要求您使用cccccccbbbbbbb的组合指定正在创建的新提交的消息-在此示例中,我将其称为ddddddd


# This is a combination of 2 commits.
# This is the 1st commit message:

Your (bad) commit you want to remove from history

# This is the commit message #2:

Your (good) commit you want to keep

Here you probably want to delete everything except the last line, which has the message of your latest commit, but feel free to customize the message however you like. 在这里,您可能希望删除除最后一行以外的所有内容,该行包含最新提交的消息,但是可以随意自定义消息。

5 . 5 Save the file, and close the text editor. 保存文件,然后关闭文本编辑器。

The rebase is done, and your history should look like this: 重新设置完成,您的历史记录应如下所示:


ddddddd (HEAD -> your-branch-name) Your (good) commit you want to keep
aaaaaaa (master) Commit from you or someone else

在推之前

6 . 6 Now, all you have to do is to send to push the branch back to your origin (eg GitHub/BitBucket/GitLab), etc. in order to update your pull-request. 现在,您要做的就是发送以将分支推回origin (例如GitHub / BitBucket / GitLab),等等,以更新您的请求请求。

For that, you'll have to do a push --force , so that your remote branch is replaced by this new one. 为此,您必须执行push --force ,以便将远程分支替换为新分支。


git push origin your-branch-name --force

最后结果

Of course, you always have the option of closing the previous pull-request and opening a new one instead (with a different branch name). 当然,您始终可以选择关闭上一个拉取请求,然后打开一个新的拉取请求(具有不同的分支名称)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM