我需要弹出并扔掉一个“中间”在我的主分支中提交。我该怎么做呢?
例如,在以下 master 分支中,我需要仅删除提交 af5c7bf16e6f04321f966b4231371b21475bc4da,这是由于之前的变基而导致的第二个提交:
commit 60b413512e616997c8b929012cf9ca56bf5c9113
Author: Luca G. Soave <[email protected]>
Date: Tue Apr 12 23:50:15 2011 +0200
add generic config/initializers/omniauth.example.rb
commit af5c7bf16e6f04321f966b4231371b21475bc4da
Author: Luca G. Soave <[email protected]>
Date: Fri Apr 22 00:15:50 2011 +0200
show github user info if logged
commit e6523efada4d75084e81971c4dc2aec621d45530
Author: Luca G. Soave <[email protected]>
Date: Fri Apr 22 17:20:48 2011 +0200
add multiple .container at blueprint layout
commit 414ceffc40ea4ac36ca68e6dd0a9ee97e73dee22
Author: Luca G. Soave <[email protected]>
Date: Thu Apr 21 19:55:57 2011 +0200
add %h1 Fantastic Logo + .right for 'Sign in with Github'
我需要维护
- 第一个提交 60b413512e616997c8b929012cf9ca56bf5c9113,
- 第三个提交 e6523efada 4d75084e81971c4dc2aec621d45530 和
- 最后一次提交 414ceffc40ea4ac36ca68e6dd0a9ee97e73dee22
“扔掉”第二次提交 af5c7bf16e6f04321f966b4231371b21475bc4da
我该怎么做?
For example, in the following master branch, I need to trash just the commit af5c7bf16e6f04321f966b4231371b21475bc4da, which is the second due to previous rebase:
commit 60b413512e616997c8b929012cf9ca56bf5c9113
Author: Luca G. Soave <[email protected]>
Date: Tue Apr 12 23:50:15 2011 +0200
add generic config/initializers/omniauth.example.rb
commit af5c7bf16e6f04321f966b4231371b21475bc4da
Author: Luca G. Soave <[email protected]>
Date: Fri Apr 22 00:15:50 2011 +0200
show github user info if logged
commit e6523efada4d75084e81971c4dc2aec621d45530
Author: Luca G. Soave <[email protected]>
Date: Fri Apr 22 17:20:48 2011 +0200
add multiple .container at blueprint layout
commit 414ceffc40ea4ac36ca68e6dd0a9ee97e73dee22
Author: Luca G. Soave <[email protected]>
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,
- the Third commit e6523efada4d75084e81971c4dc2aec621d45530 and
- the Last commit 414ceffc40ea4ac36ca68e6dd0a9ee97e73dee22
"throwing away" just the Second commit af5c7bf16e6f04321f966b4231371b21475bc4da
How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可以选择变基或恢复。 Rebase 实际上会从历史记录中删除该提交,因此看起来第二次提交从未存在过。如果您已将主分支推送到任何其他存储库,这将是一个问题。在这种情况下,如果您尝试在 rebase 之后进行推送,git 会给您一个 reject non fast-forward merges 错误。
当分支已与其他存储库共享时,恢复是正确的解决方案。 git revert af5c7bf16 将进行一次新的提交,该提交只是反转 af5c7bf16 引入的更改。这样历史就不会被重写,你可以清晰地记录错误,并且其他存储库将接受推送。
这是一个删除的好方法:
git rebase -i^
这会将您带到要删除的提交之前的提交。交互式编辑器将向您显示回到该点的所有提交的列表。您可以选择、压缩等。在这种情况下,删除您要删除的提交行并保存文件。 Rebase 将完成其工作。
Rebase or revert are the options. Rebase will actually remove the commit from the history so it will look like that second commit never existed. 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.
Revert is the correct solution when the branch has been shared with other repos.
git revert af5c7bf16
will make a new commit that simply reverses the changes that af5c7bf16 introduced. 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. 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 是一个选项,您可以 rebase 并删除它:
如果 rebase 不是一个选项,您可以将其恢复:
If rebase is an option, you can rebase and just drop it:
If rebase is not an option, you can just revert it:
尽管原始答案在这里得到了所有的认可,但我并没有找到它们来令人满意地回答这个问题。如果您发现自己处于需要从历史记录中删除一个提交或一组提交的情况,我的建议是:
以下是关于樱桃采摘的信息:
使用 git 挑选提交意味着什么?
这里有一些关于使用 Tortoise Git 进行此操作的信息(就像我刚才所做的那样)。使用 GUI 实用程序来执行此类操作绝对更容易!
使用 TortoiseGit 挑选樱桃
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:
Here's info on Cherry Picking:
What does cherry-picking a commit with git mean?
Here's some on doing it with Tortoise Git (as I just did). It's definitely easier to use a gui utility for these sorts of operations!
Cherry pick using TortoiseGit