簡體   English   中英

從PR中已經存在的PR中排除提交

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

我有提交的公開PR

我進行了另一次提交,只想通過這些更改來創建另一個PR。.但是,先前的提交在那里。

我知道分支機構可以解決這個問題,但是,現在就到了。 我是否只需要等待PR合並?

我有提交的公開PR

那時,您的存儲庫看起來像這樣。

A - B [master]
     \
      C [pr1]

提交B是master所在的位置。 並且您有一個名為pr1的分支,上面帶有提交C。

我進行了另一次提交,只想通過這些更改來創建另一個PR。.但是,先前的提交在那里。

可能發生的情況是您從pr1而不是master分支了。

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

默認情況下,為pr2創建PR時,基本分支為master因此它在PR中顯示CD


如果D的工作不依賴於C那么您想要的是pr2並提交Dmaster分支。

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

您可以通過將pr2重新pr2master上來解決此問題。

git rebase --onto master pr1 pr2

那就是說將從pr1開始(但不包括)到pr2 (包括pr2 )的提交重播到master 然后,您可以推送pr2並創建一個獨立的PR。


如果D的工作確實取決於C那么最好將pr2放在pr1之上。 pr2創建PR,其基礎為pr1而不是master pr1被審核並接受后, pr2即可被審核並接受。

確保在PR描述中記下pr2堆疊在pr1頂部,這樣審閱者就不會錯過它。 “這取決於#123”,其中#123是pr1的PR的ID。

只需從要開始的任何點創建一個新分支,然后挑選其他分支即可。 這將應用涉及另一個分支的最新修訂版的更改,而不會應用其他更改(假設它是該分支的最新修訂版)。

一種簡單的方法是使用交互式rebase

如果在分支上運行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

當前

您說要刪除bbbbbbb將其替換為ccccccc

由於cccccccbbbbbbb以來包含更改,因此刪除它會引起很多沖突,因為您必須將ccccccc合並為aaaaaaa

git方式做到這一點,是南瓜 cccccccbbbbbbb ,創建一個新的提交ddddddd 為此,您需要執行以下步驟:

1 運行git rebase -i aaaaaaa打開交互式rebase,從要刪除的提交之前立即開始提交

這將打開您配置的文本編輯器,如下所示:


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

該列表顯示了交互式rebase的作用:從aaaaaaa ,選擇bbbbbbb ,然后選擇ccccccc

2 正如我前面所說,你要擠進 cccccccbbbbbbb ,所以替換詞pickcccccccsquash


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

3 保存文件,然后關閉文本編輯器。

4 將打開另一個文本編輯器,要求您使用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

在這里,您可能希望刪除除最后一行以外的所有內容,該行包含最新提交的消息,但是可以隨意自定義消息。

5 保存文件,然后關閉文本編輯器。

重新設置完成,您的歷史記錄應如下所示:


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

在推之前

6 現在,您要做的就是發送以將分支推回origin (例如GitHub / BitBucket / GitLab),等等,以更新您的請求請求。

為此,您必須執行push --force ,以便將遠程分支替換為新分支。


git push origin your-branch-name --force

最后結果

當然,您始終可以選擇關閉上一個拉取請求,然后打開一個新的拉取請求(具有不同的分支名稱)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM