[英]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中顯示C
和D
如果D
中的工作不依賴於C
那么您想要的是pr2
並提交D
從master
分支。
D [pr2]
/
A - B [master]
\
C [pr1]
您可以通過將pr2
重新pr2
到master
上來解決此問題。
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
。
由於ccccccc
自bbbbbbb
以來包含更改,因此刪除它會引起很多沖突,因為您必須將ccccccc
合並為aaaaaaa
。
在git
方式做到這一點,是南瓜 ccccccc
到bbbbbbb
,創建一個新的提交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 。 正如我前面所說,你要擠進 ccccccc
到bbbbbbb
,所以替換詞pick
前ccccccc
由squash
:
pick bbbbbbb Your (bad) commit you want to remove from history
squash ccccccc Your (good) commit you want to keep
3 。 保存文件,然后關閉文本編輯器。
4 。 將打開另一個文本編輯器,要求您使用ccccccc
和bbbbbbb
的組合指定正在創建的新提交的消息-在此示例中,我將其稱為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.