[英]How to remove commits from a pull request
人们不希望看到错误的提交和恢复提交以撤消错误提交的更改。 这会污染提交历史。
这是删除错误提交而不是使用还原提交撤消更改的简单方法。
git checkout my-pull-request-branch
git rebase -i HEAD~n
// 其中n
是您想要包含在交互式 rebase 中的最后提交的数量。
pick
替换为drop
。git push --force
你有几种技术可以做到这一点。
这篇文章 - 阅读关于还原的部分将详细解释我们想要做什么以及如何去做。
这是解决您的问题的最简单的方法:
# Checkout the desired branch
git checkout <branch>
# Undo the desired commit
git revert <commit>
# Update the remote with the undo of the code
# The force is a tricky flag since it will force the push but
# your administrator can block it, so if it's not an option you
# can delete the old branch and push it again
git push origin <branch> --force
revert 命令将创建一个新的提交,并撤消原始提交。
如果您要删除提交并且不想保留其更改,@ferit 有一个很好的解决方案。
如果您想将该提交添加到当前分支,但作为当前 pr 的一部分没有意义,您可以执行以下操作:
git rebase -i HEAD~n
git reset HEAD^ --soft
提交更改并将它们恢复到暂存的 state 中。git push --force
在不删除提交的情况下更新远程分支。现在您将从远程删除提交,但仍将在本地进行更改。
这对我有帮助:
使用现有分支创建一个新分支。 让我们将现有的branch_old
和 new 称为branch_new
。
当您完全没有任何问题提交时,将branch_new
重置为稳定的 state。 例如,要将其置于本地大师级别,请执行以下操作:
git 复位—硬主 git 推—力原点
cherry-pick
从branch_old
到branch_new
的提交
git push
因此,请执行以下操作,
假设您的分支名称是 my_branch 并且它有额外的提交。
git checkout -b my_branch_with_extra_commits
(以不同的名称保存此分支)gitk
(打开 git 控制台)git checkout my_branch
gitk
(这将打开 git 控制台)reset branch to here
”git pull --rebase origin branch_name_to _merge_to
git cherry-pick <SHA you copied in step 3. >
现在查看本地分支提交历史并确保一切正常。
如果不小心推送了不必要的文件并想从 PR 中删除它们,那么
1. git reset <commit ID wherever you want to jump>
2. git restore <file name you want to remove> or git add <all the file
names you want to add to commit>
3. git commit -m “new commit message”
4. git push -f //necessary to push forcefully (if anything is there to pull)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.