我怎样才能合并许多提交,但留下一个提交? [英] How can I merge many commits, but leave one out?

查看:203
本文介绍了我怎样才能合并许多提交,但留下一个提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个功能分支foo。现在我想将它合并回主,但是我已经添加了一些我不想在master中调试的代码。



调试代码在它自己的提交中,所以我可以在每次提交时使用 git cherry-pick 并离开出这个提交。但那会很累人。



是否有一些逆向樱桃挑选可以做到这一点,或者是一种交互式合并?

解决方案

使用交互式rebase:

  git rebase -i SHA-OF-FIRST-COMMIT-IN- BRANCH 

这会在$ EDITOR中打开这样的内容:

 选择8ac4783文件夹和文件夹
选择cf8b1f5轻微重构
选择762b37a大量改进。文件夹和狗屎。
选择3fae6e1准备好tableview
选择b174dc0替换文件夹集合视图w /表格视图
选择ef1b65b更多完成
选择ecc407f响应者链和whatnot
选择080a847播放/暂停视频
选择6719000 wip:电影推子
选择c5f2933演示窗口淡入淡出转换

#将e6f77c8..c5f2933重新映射到e6f77c8

#命令:
#p,pick =使用提交
#e,edit =使用提交,但停止修改
#s,squash =使用提交,但融入前面的提交

#如果你在这里删除一行,那么这个提交将会丢失。
#但是,如果您删除了所有内容,那么rebase将会中止。
$




$ b因此,你所做的只是删除包含调试提交的行,写该文件并关闭你的编辑器,git会告诉你一些事情:

 成功更新和更新了refs / heads /主。 

现在您可以将该分支合并到master中。



更新:应该指出的是,改变历史与 rebase 应该 only 发生在私人分支。如果这个分支已经暴露给公众,按照其他回答者的建议使用 git revert


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.

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?

解决方案

Use interactive rebase:

git rebase -i SHA-OF-FIRST-COMMIT-IN-BRANCH

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.
    #

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.

Now you can just merge in that branch to master.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆