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
How to remove commits from a pull 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.
突出显示的提交是我需要保留并删除所有其他内容的提交。它成为历史上的第四次提交,因为我做了一些合并的东西。
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.
这是删除错误提交而不是使用还原提交撤消更改的简单方法。
git checkout my-pull-request-branch
git rebase -i HEAD~n
// wheren
is the number of last commits you want to include in interactive rebase.- Replace
pick
withdrop
for commits you want to discard. - Save and exit.
git push --force
git checkout my-pull-request-branch
git rebase -i HEAD~n
// 其中n
是您要包含在交互式变基中的最后一次提交的数量。- 更换
pick
同drop
为提交你要放弃。 - 保存并退出。
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 的一部分没有意义,您可以执行以下操作:
- use
git rebase -i HEAD~n
- Swap the commit you want to remove to the bottom (most recent) position
- Save and exit
- use
git reset HEAD^ --soft
to uncommit the changes and get them back in a staged state. - use
git push --force
to update the remote branch without your removed commit.
- 用
git rebase -i HEAD~n
- 将要删除的提交交换到底部(最近)位置
- 保存并退出
- 用于
git reset HEAD^ --soft
取消提交更改并使它们恢复到暂存状态。 - 用于
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 并且它有额外的提交。
git checkout -b my_branch_with_extra_commits
(Keeping this branch saved under a different name)gitk
(Opens git console)- Look for the commit you want to keep. Copy the SHA of that commit to a notepad.
git checkout my_branch
gitk
(This will open the git console )- Right click on the commit you want to revert to (State before your changes) and click on "
reset branch to here
" - Do a
git pull --rebase origin branch_name_to _merge_to
git cherry-pick <SHA you copied in step 3. >
git checkout -b my_branch_with_extra_commits
(保持这个分支以不同的名称保存)gitk
(打开 git 控制台)- 查找您要保留的提交。将该提交的 SHA 复制到记事本。
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. >
Now look at the local branch commit history and make sure everything looks good.
现在查看本地分支提交历史并确保一切正常。