How can I merge many commits, but leave one out?

前端 未结 5 640
醉话见心
醉话见心 2020-12-07 19:37

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 cod

5条回答
  •  旧巷少年郎
    2020-12-07 20:25

    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.

提交回复
热议问题