如何合并多个提交,但忽略一个? [英] How can I merge many commits, but leave one out?
问题描述
假设我有这个功能分支foo".现在我想将它合并回 master,但我添加了一些我不想要的调试代码.
Suppose I have this feature branch "foo". Now I want to merge it back into master, but I've added some debugging code that I don't want in master.
调试代码在它自己的提交中,所以我可以在每次提交时使用 git cherry-pick
而忽略这次提交.但这会很累.
The debug code is in it's own commit, so I could use git cherry-pick
on each commit and leave out this commit. But that's gonna be quite tiresome.
是否有一些反向樱桃选择"可以做到这一点,或者交互式合并?
Is there some "inverse cherry-pick" that does this, or an interactive merge?
推荐答案
使用交互式变基:
git rebase -i SHA-OF-FIRST-COMMIT-IN-BRANCH
这将在您的 $EDITOR 中打开类似的内容:
That will open something like this in your $EDITOR:
pick 8ac4783 folders and folders
pick cf8b1f5 minor refactor
pick 762b37a Lots of improvement. Folders adn shit.
pick 3fae6e1 Be ready to tableview
pick b174dc0 replace folder collection view w/ table view
pick ef1b65b more finish
pick ecc407f responder chain and whatnot
pick 080a847 play/pause video
pick 6719000 wip: movie fader
pick c5f2933 presentation window fade transition
# Rebase e6f77c8..c5f2933 onto e6f77c8
#
# Commands:
# p, pick = use commit
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
所以你要做的只是删除包含调试提交的行,写入文件并关闭你的编辑器,git 会告诉你一些类似的内容:
So what you do is simply to remove the line containing the debug commit, write the file and close your editor, and git will tell you something along the lines of:
Successfully rebased and updated refs/heads/master.
现在您可以在该分支中合并到 master.
Now you can just merge in that branch to master.
更新:应该注意的是,使用 rebase
更改历史记录应该只发生在私有分支上.如果此分支已公开,请按照其他回答者的建议使用 git revert
.
UPDATE: It should be noted that altering history with rebase
should only happen on private branches. If this branch has been exposed to the public, use git revert
as proposed by other answerer.
这篇关于如何合并多个提交,但忽略一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!