git 如何合并多个提交,但忽略一个?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1624705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:16:10  来源:igfitidea点击:

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

gitmerge

提问by Dave Vogt

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.

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

The debug code is in it's own commit, so I could use git cherry-pickon each commit and leave out this commit. But that's gonna be quite tiresome.

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

Is there some "inverse cherry-pick" that does this, or an interactive merge?

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

采纳答案by Harry Vangberg

Use interactive rebase:

使用交互式变基:

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

That will open something like this in your $EDITOR:

这将在您的 $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:

所以你要做的只是删除包含调试提交的行,写入文件并关闭你的编辑器,git 会告诉你一些类似的内容:

Successfully rebased and updated refs/heads/master.

Now you can just merge in that branch to master.

现在你可以在那个分支中合并到 master。

UPDATE: It should be noted that altering history with rebaseshould onlyhappen on private branches. If this branch has been exposed to the public, use git revertas proposed by other answerer.

更新:应该注意的是,更改历史记录rebase应该发生在私有分支上。如果此分支已向公众公开,请git revert按照其他回答者的建议使用。

回答by CB Bailey

Despite what other SCMs use it to mean, in git, git revertis an inverse cherry-pick.

尽管其他 SCM 使用它来表示,在 中git,它git revert是一个逆樱桃选择。

回答by Paul Pladijs

Another idea is to add the reverted commit of the one with debug code and to merge it into your master branch. Afterwards we remove that extra commit in the foo branch.

另一个想法是添加带有调试代码的还原提交并将其合并到您的主分支中。之后,我们删除 foo 分支中的额外提交。

git checkout foo
git revert COMMIT_REF_WITH_DEBUG_CODE

git checkout master
git merge foo

git checkout foo
git reset --hard HEAD~1

Make sure your working tree is clean. First create the reverted commit. Then merge it into master. Afterwards reset the branch pointer of the foo branch to the parent of the reverted commit, so it's back in its original state.

确保您的工作树是干净的。首先创建还原的提交。然后将其合并到 master 中。之后将 foo 分支的分支指针重置为恢复提交的父级,因此它返回到原始状态。

If you dislike using git resetthen you could create a temporary branch where you create the reverted commit. At the end you delete the temporary branch.

如果您不喜欢使用,git reset则可以创建一个临时分支,在其中创建还原提交。最后删除临时分支。

回答by Yang Zhao

Use interactive rebase to remove the commits which you do not want.

使用交互式变基删除您不想要的提交。

On a new branch "foo-merge" created from "foo":

在从“foo”创建的新分支“foo-merge”上:

git rebase -i master

Once you are in commit edit mode, remove the lines containing the debug commits, save, and quit from the editor.

进入提交编辑模式后,删除包含调试提交的行,保存并退出编辑器。

After rebasing, simply pull foo-merge into master:

变基后,只需将 foo-merge 拉入 master:

git checkout master
git pull . foo-merge

回答by michaeljoseph

I've had success with:

我在以下方面取得了成功:

git rebase -p --onto SHA^ SHA

Where SHAis the commit you want to remove.

SHA您要删除的提交在哪里。

via http://sethrobertson.github.io/GitFixUm/fixup.html#remove_deep

通过http://sethrobertson.github.io/GitFixUm/fixup.html#remove_deep