如何从拉请求中删除提交

我做了一个拉请求,但之后我在本地对项目进行了一些提交,最终污染了我的拉请求,我试图删除它,但没有任何运气。

我在StackOverflow上找到了一些类似的问题,但我不能应用在那里的东西。 这是我在GitHub上的第一个拉请求,所以对我来说这一切是如何工作的有点奇怪 突出显示的提交是我需要保留并删除所有其他内容的提交。 它成为历史上的第四次提交,因为我做了一些合并的东西

enter image description here

我的git日志 enter image description here < / p >

有人能解释一下发生了什么以及如何解决这个问题吗?

264995 次浏览

所以做下面的事情,

假设你的分支名为my_branch,它有额外的提交。

  1. git checkout -b my_branch_with_extra_commits(将这个分支保存在不同的名称下)
  2. gitk(打开git控制台)
  3. 寻找你想要兑现的承诺。将该提交的SHA复制到记事本。
  4. git checkout my_branch
  5. gitk(这将打开git控制台)
  6. 右键单击要恢复的提交(更改前的状态),然后单击“reset branch to here”。
  7. 做一个git pull --rebase origin branch_name_to _merge_to
  8. git cherry-pick <SHA you copied in step 3. >

现在查看本地分支提交历史并确保一切正常。

你有好几种方法可以做到。

这篇文章-阅读关于恢复的部分将详细解释我们想要做什么以及如何做。

这里有一个最简单的解决方法:

# 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命令将使用原始提交的撤销创建一个新的提交。

人们不希望看到一个错误的提交和一个恢复提交来撤销错误提交的更改。这玷污了历史。

下面是一种简单的方法,用于删除错误的提交,而不是使用恢复提交来撤销更改。

  1. git checkout my-pull-request-branch

  2. git rebase -i HEAD~n //其中n是你想要包含在交互式中的最后一次提交的数量 李变基。< / p > < / >

  3. 对于想要丢弃的提交,将pick替换为drop
  4. 保存并退出。
  5. git push --force

如果你正在删除一个提交,并且不想保留它的更改,@ferit有一个很好的解决方案。

如果你想把这个commit添加到当前分支,但是作为当前pr的一部分没有意义,你可以做下面的事情:

  1. 使用git rebase -i HEAD~n
  2. 将要删除的提交交换到底部(最近的)位置
  3. 保存并退出
  4. 使用git reset HEAD^ --soft来取消提交更改,并将它们恢复到阶段性状态。
  5. 使用git push --force来更新远程分支而不删除提交。

现在您已经从远程删除了提交,但在本地仍然有更改。

这对我很有帮助:

  1. 用现有的分支创建一个新的分支。让我们将现有的命名为branch_old,将新的命名为branch_new

  2. branch_new重置为稳定状态,当你提交时没有任何问题。 例如,要将它放在您本地的主级别,请执行以下操作:

    git reset -hard master Git push -force origin

  3. cherry-pickbranch_old提交到branch_new

  4. git push

如果不小心推送了不必要的文件,并希望将它们从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)

从你的分支开火以下命令

git reset --soft origin/master
git commit -m "Detail of Your last commit that you want to see"
git push -f