简体   繁体   English

从 Git 中的 PR 中删除提交

[英]Remove a Commit from a PR in Git

I created a PR but accidentally branched off the wrong commit.我创建了一个 PR 但不小心分支了错误的提交。

commit 4
commit 3
commit 2
commit 1 <-- Want to get rid of first commit  

Commit 1 is actually part of another PR. Commit 1实际上是另一个 PR 的一部分。 I want to get rid of the first commit because it has nothing to do with the PR i created.我想摆脱第一次提交,因为它与我创建的 PR 无关。 What is the best way to go about this?解决这个问题的最佳方法是什么?

The simplest, don't-really-have-to-think-about-it way is usually an interactive rebase.最简单的、不需要考虑的方式通常是交互式 rebase。 Do git rebase -i <pr-base> (long: git rebase --interactive <pr-base> ), delete the first line, save and quit.执行git rebase -i <pr-base> (long: git rebase --interactive <pr-base> ),删除第一行,保存退出。 That'll rewrite commit 1 out of history.这将重写历史记录中的提交 1。 Then it's probably git push --force-with-lease as usual to sync the changes in your case.然后它可能像往常一样git push --force-with-lease来同步您的案例中的更改。

This will remove the commit like it never was there ie the changes will be undone and it will be removed from history for that branch.这将删除从未有过的提交,即更改将被撤消,并将从该分支的历史记录中删除。 Logically this will not work if newer commits are based on changes in the commit you want to remove.从逻辑上讲,如果较新的提交基于您要删除的提交中的更改,则这将不起作用。 (Git will tell you about this "conflict".) (Git 会告诉你这个“冲突”。)

On the branch of your pull request:在您的拉取请求的分支上:

  1. git rebase HEAD~4 --interactive (4 is the number of commits to show) git rebase HEAD~4 --interactive (4 是要显示的提交数)
  2. change the first word from pick to drop for the commit you want to remove.对于要删除的提交,将第一个单词从pick更改为drop [Warning: Risk of data loss. [警告:数据丢失风险。 Make sure this commit is part of another branch if you do not want to lose its content.]如果您不想丢失其内容,请确保此提交是另一个分支的一部分。]
  3. Save and close the "file".保存并关闭文件”。
  4. git push --force (This will forcefully overwrite the remote. Imo that is no problem if you do not expect anyone else working on this remote branch.) git push --force (这将强行覆盖远程。如果你不希望其他人在这个远程分支上工作,我没问题。)

(Well it looks like I was too slow.) (好吧,看来我太慢了。)

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

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