git - 合并时跳过特定提交

Bra*_*son 190 git version-control

我已经使用Git大约一年了,并认为这太棒了,但我刚刚开始了该项目的第二个版本,并开始了一个新的分支.我正在努力处理未来的最佳方法.

我有两个分支叫做master10(对于v1)和master20(对于v2).我一直在分支master10上的v1中修复bug,并开发master20的新东西.每当我修复bug时,我都会通过检查master20并将其合并到v2中git merge master10.到现在为止还挺好.

然而,现在我在v1中做了一个我不想要的改变,但是我想继续合并其他错误修复.我如何告诉Git跳过该特定提交(或一系列提交),但是未来我仍然希望合并其他错误修复.

我认为git rebase可能是我需要的,但阅读文档和我的头几乎爆炸.

我想我想要的就像是一个"git sync"命令,它告诉git两个分支现在是同步的,并且将来只会合并来自这个同步点的提交.

任何帮助赞赏.

ara*_*nid 280

例如,如果要将分支"maint"上的大多数提交但不是所有提交合并到"master",则可以执行此操作.它需要一些工作----如上所述,通常的用例是合并一个分支中的所有内容---但有时会发生一个不应该集成的发布版本的更改(可能是代码的已经被大师取代了),你怎么代表呢?开始...

因此,假设maint已经应用了5个更改,其中一个(maint~3)不会合并回master,尽管其他所有应该都是.你在三个阶段完成这个任务:实际上在那之前合并一切,告诉git将maint~3标记为合并,即使它不合并,然后合并其余部分.神奇的是:

bash <master>$ git merge maint~4
bash <master>$ git merge -s ours maint~3
bash <master>$ git merge maint
Run Code Online (Sandbox Code Playgroud)

第一个命令将麻烦的maint提交之前的所有内容合并到master上.默认的合并日志消息将解释您正在合并"branch'maint'(早期部分)".

第二个命令合并麻烦的maint~3 commit,但是"-s ours"选项告诉git使用一个特殊的"合并策略",实际上,只需保留你要合并的树并忽略提交即可.你完全融合了.但是它仍然使用HEAD和maint~3作为父项进行新的合并提交,因此修订图现在表示maint~3被合并.所以实际上你可能也想使用-m选项'git merge'来解释maint~3 commit实际上被忽略了!

最后一个命令只是将其余的maint(maint~2..maint)合并到master中,这样你就可以再次同步了.

  • @MarkBooth:您想要跳过的提交可能与要合并的分支发生巨大冲突.更容易跳过它而不是修复冲突然后恢复该更改并再次修复冲突 (14认同)
  • 只是出于兴趣,如果您只想省略一个更改,为什么不只是执行一次合并然后还原一个更改集,而不是执行三个合并?这将导致更少的更改集提交到存储库并显示更明确的历史记录. (5认同)
  • 我认为如果你需要推迟合并那个提交,你别无选择,只能跳过它(合并我们的),然后使用cherry-pick命令应用它.一旦提交可以从master获得,它就不能再次合并 - 避免合并相同的更改两次是主要的git目标之一. (4认同)
  • 关于你的支行示例:我怀疑,一旦你合并了子支路,你的情况与我发布的第二步后的情况完全相同.创建其他分支对提交关系没有任何影响. (3认同)
  • 通常,显式命名提交更容易.所以你只需要查看要合并的分支的git日志,并注意不应该合并的提交的哈希值和之前的哈希值 - 而不是计算提交... (3认同)
  • 出色的。这正是我一直在寻找的。我现在需要做的就是,当我在 v1 中进行更改时,我想要在 v2 中进行更改,则为 git merge master10,而当我想忽略更改时,则为 git merge -s ours master10。完美的。“git merge -s ours”是我在OP中建议的“sync”命令。谢谢! (2认同)

Ale*_* T. 34

IMHO, the most logical thing to do, is to merge everything, and then use git revert (commit_you_dont_want) to remove it.

Example:

git merge master
git revert 12345678
Run Code Online (Sandbox Code Playgroud)

If you have multiple "to-ignore" commits, or would like to edit revert message:

git merge master
git revert -n 123456
git revert -n abcdef
git commit -m "... Except commits 123456 and abcdef"
Run Code Online (Sandbox Code Playgroud)

Then your history may look like:

| ... Except 123456 and abcdef
|\ Merge branch 'master' into 'your_branch'
Run Code Online (Sandbox Code Playgroud)

If you have conflicts involving ONLY these "to-ignore" commits, you may use:

git merge master -X ours
Run Code Online (Sandbox Code Playgroud)

So your version will persist over the other one. Even without error messages, you may still "revert" those unwanted commits, because they may have other changes that did not conflict, and you still don't want them.

If you have conflicts envolving NOT ONLY the "to-ignore" commits, you should resolve them manually, and you'll probably have to resolve them again during reverting.

  • 如果您随后想要稍后将那些还原的提交合并到分支中,则Git仍然会忽略它们吗? (2认同)
  • @AnriëtteMyburgh,您可以恢复还原提交,以便稍后将它们合并到其中 - 实际上您无法使用“merge -s ours”策略轻松做到这一点。 (2认同)

Dus*_*tin 17

提交包括祖先.如果不合并先前的提交,则无法合并提交.

当然,你可以挑选它们.当你的分支处于维护模式时,这是一个很好的流程.


小智 6

听起来像'git cherry-pick'的经典案例 https://git-scm.com/docs/git-cherry-pick 它完全听起来像听起来像

  • “听起来像是”top 的经典案例:D (2认同)