简体   繁体   English

从我的git历史中删除4个提交

[英]Remove 4 commits from my git history

I have done some commits and pushed them into my repository. 我做了一些提交并将它们推送到我的存储库中。 Then I did a pull request but I realized there are some commits I don't want to be in the pull request. 然后我做了拉取请求,但我意识到有些提交我不想在拉请求中。

They look like this: 它们看起来像这样:

My commits look like this:

Correct HTML    
ab1c41c 

HTML escaping   
8b38955 

Merge branch 'master' into internationalized    
2854662

Modified config 
b942f13 

tried pushing   
b73d792 

Added assets    
f20106e 

Added config    
408118f 

Fixed views conflicts   
86f2509 

added layouts   
da27e11 

Fixed layout markup
92d6bcc 

If I want to get rid of these commits: 如果我想摆脱这些提交:

Modified config 
b942f13 

tried pushing   
b73d792 

Added assets    
f20106e 

Added config    
408118f 

How do I do it? 我该怎么做? Notice that I want to keep these ones which are before in time that the ones i want to delete: 请注意,我想保留那些我想删除的那些之前的那些:

Correct HTML    
ab1c41c 

HTML escaping   
8b38955

You can do an interactive rebase . 你可以做一个互动的rebase

Assuming your head is at ab1c41c from your example, invoke the rebase as follows 假设您的头部位于示例中的ab1c41c ,请按如下方式调用rebase

git rebase -i HEAD~7

This tells git you want to manipulate the last 8 or so commits. 这告诉git你想操纵最后8个左右的提交。 You'll be dropped into your editor with a listing of the commits and some instructions. 您将被删除到您的编辑器中,其中包含提交列表和一些说明。

Delete the lines that contain the commits to remove, save and quit. 删除包含要删除,保存和退出的提交的行。 Git will preform the rebase, and that's it. Git会预先制作rebase,就是这样。

Keep in mind, because of the rebase, if you want to push to the same branch you'll need to pass the option --force . 请记住,由于rebase,如果你想推送到同一个分支,你需要传递选项--force

Disclaimer Rebasing and force pushing can cause you to lose work or piss people off, so just make sure you understand what you're doing. 免责声明重新定位和强行推动会导致您失去工作或者让人失望,所以请确保您了解自己在做什么。 :) :)

As Blake Taylor mentioned here , you can use interactive rebase to reorder commits (or) remove intermediate commits. 正如Blake Taylor在这里提到的,您可以使用交互式rebase重新排序提交(或)删除中间提交。

But that must be done, before you push those commits into public repository. 但在将这些提交推送到公共存储库之前,必须这样做。 Refer here . 请参考这里 In your case, those commits are already available in the public repo. 在您的情况下,这些提交已经在公共回购中提供。 So, I don't suggest using rebase. 所以,我不建议使用rebase。

If you don't want those commits in your working directory. 如果您不希望在工作目录中提交这些提交。

  • a) Create a branch, which points to 86f2509 (the last stable commit in your working tree). a)创建一个分支,指向86f2509(工作树中的最后一个稳定提交)。

    git checkout -b <branch name> 86f2509

  • b) Cherry Pick those commits you want. b) Cherry Pick你想要的那些提交。 In your case ab1c41c and 8b38955 . 在您的情况下ab1c41c8b38955

    git cherry-pick 8b38955 ab1c41c

Now your working directory will not have those unwanted commits. 现在你的工作目录不会有那些不需要的提交。

Just reset with --hard on the commit just before: 只需在之前的提交中使用--hard重置:

git reset --hard 86f2509

Beware: you'll lose those commits forever, no way to go back. 当心:你将永远失去那些承诺,无法回头。 Also, if someone has pulled in the commits you want to get rid of, things may get messy. 此外,如果某人已经提交了您想要摆脱的提交,事情可能会变得混乱。 In that case, you'd probably want to look at 'git revert'. 在这种情况下,你可能想看看'git revert'。

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

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