git 如何从拉取请求中删除提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36168839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:03:54  来源:igfitidea点击:

How to remove commits from a pull request

gitgithubpull-request

提问by humazed

I did a pull request but after that I made some commits to the project locally which ended polluting my pull request, I tried to remove it but without any luck.

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

I found some similar questions on StackOverflow but I can't apply what's in there. It's my first pull request on GitHub so it's kinda strange to me how all of this works.

我在 StackOverflow 上发现了一些类似的问题,但我无法应用其中的内容。这是我在 GitHub 上的第一个拉取请求,所以我觉得这一切是如何工作的有点奇怪。

The highlighted commit is the one I need to keep and remove all the other stuff. It becomes the fourth commit in the history because I make some merge stuff.

突出显示的提交是我需要保留并删除所有其他内容的提交。它成为历史上的第四次提交,因为我做了一些合并的东西。

enter image description here

enter image description here

my git log enter image description here

我的 git 日志 enter image description here

Can someone please explain what's going on and how to fix this problem?

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

回答by CodeWizard

You have several techniques to do it.

你有几种方法可以做到这一点。

This post - read the part about the revertwill explain in details what we want to do and how to do it.

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

Here is the most simple solution to your problem:

这是解决您问题的最简单方法:

# Checkout the desired branch
git checkout <branch>

# Undo the desired commit
git revert <commit>

# Update the remote with the undo of the code
git push origin <branch>

The revert command will create a new commit with the undoof the original commit.

revert 命令将创建一个新的提交,并撤消原始提交。

回答by ferit

People wouldn't like to see a wrong commit and a revert commit to undo changes of the wrong commit. This pollutes commit history.

人们不希望看到错误提交和还原提交来撤消错误提交的更改。这会污染提交历史。

Here is a simple way for removing the wrong commit instead of undoing changes with a revert commit.

这是删除错误提交而不是使用还原提交撤消更改的简单方法。

  1. git checkout my-pull-request-branch

  2. git rebase -i HEAD~n// where nis the number of last commits you want to include in interactive rebase.

  3. Replace pickwith dropfor commits you want to discard.
  4. Save and exit.
  5. git push --force
  1. git checkout my-pull-request-branch

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

  3. 更换pickdrop为提交你要放弃。
  4. 保存并退出。
  5. git push --force

回答by John

If you're removing a commit and don't want to keep its changes @ferit has a good solution.

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

If you want to add that commit to the current branch, but doesn't make sense to be part of the current pr, you can do the following instead:

如果您想将该提交添加到当前分支,但作为当前 pr 的一部分没有意义,您可以执行以下操作:

  1. use git rebase -i HEAD~n
  2. Swap the commit you want to remove to the bottom (most recent) position
  3. Save and exit
  4. use git reset HEAD^ --softto uncommit the changes and get them back in a staged state.
  5. use git push --forceto update the remote branch without your removed commit.
  1. git rebase -i HEAD~n
  2. 将要删除的提交交换到底部(最近)位置
  3. 保存并退出
  4. 用于git reset HEAD^ --soft取消提交更改并使它们恢复到暂存状态。
  5. 用于git push --force在没有删除提交的情况下更新远程分支。

Now you'll have removed the commit from your remote, but will still have the changes locally.

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

回答by Som Bhattacharyya

So do the following ,

因此,请执行以下操作,

Lets say your branch name is my_branch and this has the extra commits.

假设你的分支名称是 my_branch 并且它有额外的提交。

  1. git checkout -b my_branch_with_extra_commits(Keeping this branch saved under a different name)
  2. gitk(Opens git console)
  3. Look for the commit you want to keep. Copy the SHA of that commit to a notepad.
  4. git checkout my_branch
  5. gitk(This will open the git console )
  6. Right click on the commit you want to revert to (State before your changes) and click on "reset branch to here"
  7. Do a git pull --rebase origin branch_name_to _merge_to
  8. git cherry-pick <SHA you copied in step 3. >
  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. >

Now look at the local branch commit history and make sure everything looks good.

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