简体   繁体   English

我需要在我的主分支中弹出并删除“中间”提交。我该怎么做?

[英]I need to pop up and trash away a “middle” commit in my master branch. How can I do it?

For example, in the following master branch, I need to trash just the commit af5c7bf16e6f04321f966b4231371b21475bc4da, which is the second due to previous rebase: 例如,在下面的主分支中,我需要删除提交af5c7bf16e6f04321f966b4231371b21475bc4da,这是由于之前的rebase导致的第二个:

commit 60b413512e616997c8b929012cf9ca56bf5c9113
Author: Luca G. Soave <luca.soave@gmail.com>
Date:   Tue Apr 12 23:50:15 2011 +0200

    add generic config/initializers/omniauth.example.rb

commit af5c7bf16e6f04321f966b4231371b21475bc4da
Author: Luca G. Soave <luca.soave@gmail.com>
Date:   Fri Apr 22 00:15:50 2011 +0200

    show github user info if logged

commit e6523efada4d75084e81971c4dc2aec621d45530
Author: Luca G. Soave <luca.soave@gmail.com>
Date:   Fri Apr 22 17:20:48 2011 +0200

    add multiple .container at blueprint layout

commit 414ceffc40ea4ac36ca68e6dd0a9ee97e73dee22
Author: Luca G. Soave <luca.soave@gmail.com>
Date:   Thu Apr 21 19:55:57 2011 +0200

    add %h1 Fantastic Logo + .right for 'Sign in with Github'

I need to mantain 我需要保持健康

  • the First commit 60b413512e616997c8b929012cf9ca56bf5c9113, 第一次提交60b413512e616997c8b929012cf9ca56bf5c9113,
  • the Third commit e6523efada4d75084e81971c4dc2aec621d45530 and 第三次提交e6523efada4d75084e81971c4dc2aec621d45530
  • the Last commit 414ceffc40ea4ac36ca68e6dd0a9ee97e73dee22 最后一次提交414ceffc40ea4ac36ca68e6dd0a9ee97e73dee22

"throwing away" just the Second commit af5c7bf16e6f04321f966b4231371b21475bc4da “扔掉”只是第二次提交af5c7bf16e6f04321f966b4231371b21475bc4da

How can I do that? 我怎样才能做到这一点? Thanks in advance Luca 在此先感谢Luca

Rebase or revert are the options. Rebaserevert是选项。 Rebase will actually remove the commit from the history so it will look like that second commit never existed. Rebase实际上会从历史记录中删除提交,因此看起来第二次提交从未存在过。 This will be a problem if you've pushed the master branch out to any other repos. 如果您将主分支推送到任何其他存储库,这将是一个问题。 If you try to push after a rebase in this case, git will give you a reject non fast-forward merges error. 如果在这种情况下尝试推送一个rebase,git会给你一个拒绝非快进合并错误。

Revert is the correct solution when the branch has been shared with other repos. 当分支与其他repos共享时,Revert是正确的解决方案。 git revert af5c7bf16 will make a new commit that simply reverses the changes that af5c7bf16 introduced. git revert af5c7bf16将进行一个新的提交,简单地反转af5c7bf16引入的更改。 This way the history is not rewritten, you maintain a clear record of the mistake, and other repos will accept the push. 这种方式历史不会被重写,你保持清楚的错误记录,其他的回购将接受推动。

Here's a good way to erase: git rebase -i <commit>^ That takes you to the commit just before the one you want to remove. 这是一个很好的擦除方法: git rebase -i <commit>^这会将您带到要删除的提交之前的提交。 The interactive editor will show you a list of all the commits back to that point. 交互式编辑器将显示返回该点的所有提交的列表。 You can pick, squash, etc. In this case remove the line for the commit you want to erase and save the file. 您可以选择,压缩等。在这种情况下, 删除要删除的提交行并保存文件。 Rebase will finish its work. Rebase将完成其工作。

If rebase is an option, you can rebase and just drop it: 如果rebase是一个选项,你可以改变并删除它:

$ git rebase -i 414ceffc^

If rebase is not an option, you can just revert it: 如果不能选择rebase,你可以恢复它:

$ git revert af5c7bf16

Despite all the credit the original answers received here, I didn't quite find them to satisfactorily answer the question. 尽管这里收到了原始答案的所有功劳,但我并没有发现它们能够令人满意地回答这个问题。 If you find yourself in a situation where you need to remove a commit, or a collection of commits from the middle of the history, this is what I suggest: 如果您发现自己处于需要从历史中间删除提交或提交集合的情况,我建议这样做:

  • Create a new branch off the head of the one containing all the commits and switch to it. 在包含所有提交的分支的头部创建一个新分支并切换到它。
  • Revert the new branch back to the point you want to start a new base from. 将新分支恢复到您想要从中开始新基础的点。
  • Then, (here's the key point) cherry pick the subsequent commits you actually want to be applied after that from the original branch to the new one, and skip the commits you don't want anymore (ie the ones you are deleting). 然后,(这里是关键点) 樱桃选择你之后实际想要从原始分支到新分支的后续提交,并跳过你不再需要的提交(即你要删除的提交)。
  • If desired, rename the original branch to something indicating it's old code, and then rename your new branch what the original one was called. 如果需要,将原始分支重命名为指示其旧代码的内容,然后将新分支重命名为原始分支的名称。
  • Finally, push your changes to your remote repo (if using one). 最后,将您的更改推送到远程仓库(如果使用的话)。 You'll probably need to use a "forced push". 您可能需要使用“强制推送”。 If your collaborators have issues pulling the revisions, it might be easiest for them to just clone the repo again from the remote source. 如果您的协作者在修改版本时遇到问题,那么他们可能最容易从远程源再次克隆回购。 One way or another, you'll likely want to talk to them if you are ripping commits out of the middle of your history anyway! 无论如何,如果您正在从历史中间删除提交,您可能会想要与他们交谈!

Here's info on Cherry Picking: What does cherry-picking a commit with git mean? 这里有关于Cherry Picking的信息: 使用git意味着采摘樱桃是什么意思?

Here's some on doing it with Tortoise Git (as I just did). 这里有一些用Tortoise Git做的(正如我刚才所做的那样)。 It's definitely easier to use a gui utility for these sorts of operations! 在这些操作中使用gui实用程序肯定更容易! Cherry pick using TortoiseGit Cherry挑选使用TortoiseGit

暂无
暂无

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

相关问题 我需要将最近的10次提交放在一个单独的分支上。 然后我想直接在master上的这10个之前处理提交 - I need to put my most recent 10 commits on a separate branch. Then I want to work off the commit directly before these 10 on master 我克隆了一个远程分支。 我如何结帐大师? - I cloned a remote branch. How do I checkout master? 我在错误的分支上进行了更改。 我还没有承诺。 如何将我的更改提交到预期的分支? - I made changes while on the wrong branch. I haven't committed yet. How do commit my changes to the intended branch? 不小心提交到主分支而不是开发分支。 现在我的主分支在开发分支之前。 我该如何解决? - Accidentally commited to main branch instead of develop branch. Now my main branch is ahead of the develop branch. How can I fix it? 我有一个远程起源分支。 在本地,我有一个主分支和测试分支。 如何在不提交的情况下拉到测试分支? - I have a remote origin branch. Locally I have a master branch and test branch. How to pull to test branch without committing? 如何将分支恢复为主节点并丢弃提交? - How do I revert my branch to the master and throw away my commits? 在将我的任何功能分支提交推送到远程之前,我是否需要重新设置为 master - Do I need to rebase to master before pushing any of my feature branch commit to remote 如何通过git让我的分支与master保持同步? - How can I keep my branch up to date with master with git? 如何在一个git commit中将master分支的更改合并到我的side project分支中? - How do I merge a master branch's changes into my side project branch in one git commit? 当前不在任何分支上 + git commit + checkout 不在任何分支上。 我丢失了我的更改吗? - Not currently on any branch + git commit + checkout when not in any branch. Did I lose my changes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM