我如何在git中预览合并? [英] How can I preview a merge in git?

查看:170
本文介绍了我如何在git中预览合并?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个git分支(例如mainline),我想在另一个开发分支中合并。或者我?

为了决定我是否真的想合并这个分支,我希望看到合并将做的某种预览。最好能够看到正在应用的提交列表。

到目前为止,我能想到的最好的是 merge - no-ff --no-commit ,然后 diff HEAD

解决方案

我发现最适合我的解决方案是只需执行合并,并在不喜欢结果的情况下中止合并。这个特殊的语法对我来说是干净和简单的。如果你想确保你不会混淆你当前的分支,或者你只是没有准备好合并,不管冲突是否存在,只需创建一个新的子分支并合并即可。



策略1:安全的方式 - 合并临时分支:



  git checkout mybranch 
git checkout -b mynew-temporary-branch
git merge some-other-branch



如果你只是想看看冲突是什么,那么你可以简单地扔掉临时分支。您不必费心中止合并,并且可以返回工作 - 只需再次签出'mybranch',并且您的分支中不会有任何合并的代码或合并冲突。

这基本上是空运。

策略2:当你一定要合并时,但只有在没有冲突的情况下

  git checkout mybranch 
git merge some-other-branch

如果git报告发生冲突(并且只有在 冲突时),才可以执行: code> git merge --abort

如果合并成功,您不能中止它(只重置)。



如果您尚未准备好合并,请使用上面更安全的方式。



<编辑:2016年11月 - 我将策略1换成2,因为它似乎是大多数人正在寻找安全的方式。策略2现在更像是一个说明,如果合并存在冲突,您可以简单地中止合并,而您尚未准备好处理这些冲突。请记住,如果阅读评论!]


I have a git branch (the mainline, for example) and I want to merge in another development branch. Or do I?

In order to decide whether I really want to merge this branch in, i'd like to see some sort of preview of what the merge will do. Preferably with the ability to see the list of commits that are being applied.

So far, the best I can come up with is merge --no-ff --no-commit, and then diff HEAD.

解决方案

I've found that the solution the works best for me is to just perform the merge and abort it if you don't like the results. This particular syntax feels clean and simple to me. If you want to ensure you don't mess up your current branch, or you're just not ready to merge regardless of the existence of conflicts, simply create a new sub-branch off of it and merge that.

Strategy 1: The safe way – merge off a temporary branch:

git checkout mybranch
git checkout -b mynew-temporary-branch
git merge some-other-branch

That way you can simply throw away the temporary branch if you just want to see what the conflicts are. You don't need to bother "aborting" the merge, and you can go back to your work -- simply checkout 'mybranch' again and you won't have any merged code or merge conflicts in your branch.

This is basically a dry-run.

Strategy 2: When you definitely want to merge, but only if there aren't conflicts

git checkout mybranch
git merge some-other-branch

If git reports conflicts (and ONLY IF THERE ARE conflicts) you can then do:

git merge --abort

If the merge is successful, you cannot abort it (only reset).

If you're not ready to merge, use the safer way above.

[EDIT: 2016-Nov - I swapped strategy 1 for 2, because it seems to be that most people are looking for "the safe way". Strategy 2 is now more of a note that you can simply abort the merge if the merge has conflicts that you're not ready to deal with. Keep in mind if reading comments!]

这篇关于我如何在git中预览合并?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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