git 如何在不实际合并的情况下测试合并

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7484199/
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 11:52:47  来源:igfitidea点击:

How to test a merge without actually merging first

gitmerge

提问by dole doug

Is there any way of simulating a git mergebetween two branches, the current working branch and the master, but without making any changes?

有没有办法模拟git merge两个分支之间的a ,当前工作分支和主分支,但不做任何更改?

I often have conflicts when I have to make a git merge. Is there any way of simulating the merge first?

当我必须制作一个git merge. 有没有办法先模拟合并?

采纳答案by Mark Longair

I don't think there is a way of simulating what will happen until you try the merge. However, if you make sure that the output of git statusis empty before you do the merge, it is quite safe to just go ahead and try it. If you get conflicts, you can immediately get back to the state you were at before with:

我不认为有一种方法可以模拟在您尝试合并之前会发生什么。但是,如果您git status在进行合并之前确保 的输出为空,那么继续尝试是非常安全的。如果遇到冲突,您可以立即恢复到之前的状态:

git reset --merge

Since git 1.7.4, you can also abort the merge by doing:

从 git 1.7.4 开始,您还可以通过执行以下操作来中止合并:

git merge --abort

(As the commit message that added that option explains, this was added for consistency with git rebase --abortand so on.)

(正如添加该选项的提交消息所解释的,这是为了与git rebase --abort等等保持一致而添加的。)

回答by Amber

You can use git merge --no-committo prevent the merge from actually being committed, and if you don't like how the merge works out, just reset to the original head.

您可以使用git merge --no-commit来阻止实际提交合并,如果您不喜欢合并的方式,只需重置为原始头。

If you definitely don't want to finalize the merge, even if it's a fast-forward (and thus has no conflicts, by definition), you could add --no-ffas well.

如果您绝对不想完成合并,即使它是快进的(因此根据定义没有冲突),您也可以添加--no-ff

回答by Ian

If I want to compare changes on a topic branch to master, I find it easiest and safest to do the following:

如果我想将主题分支上的更改与 master 进行比较,我发现执行以下操作最简单和最安全:

git checkout master
git checkout -b trial_merge
git merge topic_branch

After completing the merge, it is easy to see the consolidated change from master

完成合并后,很容易从master看到合并后的变化

git diff master

When done, simply delete the trial_merge branch

完成后,只需删除 trial_merge 分支

git checkout master
git branch -D trial_merge

This way, the master branch never changes.

这样,主分支永远不会改变。

回答by Ortomala Lokni

I use :

我用 :

git merge --ff-only

according to documentation:

根据文档

Refuse to merge and exit with a non-zero status unless the current HEAD is already up-to-date or the merge can be resolved as a fast-forward.

拒绝合并并以非零状态退出,除非当前 HEAD 已经是最新的或者合并可以解决为快进。

It's not really a simulation because there will be a fast-forward merge in case of no conflicts between the two branches. But in case of conflicts, you will be informed and nothing will happens.

这不是真正的模拟,因为在两个分支之间没有冲突的情况下会进行快进合并。但如果发生冲突,您会收到通知,并且不会发生任何事情。

回答by bean5

I've been able to use git merge --abort, recently. However, this can only be used if there is a merge conflict. If you are sure that you will not want to commit, then use the other mentioned methods above.

git merge --abort最近可以用了 但是,这只能在存在合并冲突时使用。如果您确定不想提交,请使用上面提到的其他方法。

回答by Mohan S Nayaka

Why not just create a throwaway branch (git checkout -b), and do a test merge there?

为什么不创建一个一次性分支(git checkout -b),然后在那里进行测试合并?

回答by Lucas Amorim Silva

I don't know exactly if it is your case, but your question remember me that sometimes I start a feature, I commit over the days and I merge the develop on it many times.

我不知道这是否是您的情况,但您的问题请记住我,有时我会启动一个功能,我会在几天内提交并多次合并开发。

On this point I lose the control over the exact files I changed and I will only know it when my feature were closed and my code go to develop.

在这一点上,我失去了对我更改的确切文件的控制,只有当我的功能关闭并且我的代码开始开发时,我才会知道它。

In this case, a good way to know what modifications you did (not other from the merges) is using Sourcetree.

在这种情况下,了解您所做的修改(不是合并中的其他修改)的一个好方法是使用 Sourcetree。

You must click with the right button on the base branch and select Diff Against Current:

您必须在基本分支上单击右键并选择Diff Against Current

Sourcetree's feature to know the diff between two branches

Sourcetree 的功能可以知道两个分支之间的差异

Then sourcetree will show you all the modifications that will be merged if you merge your branch into base branch.

然后,如果将分支合并到基本分支,sourcetree 将显示将合并的所有修改。

Results

结果

Of course, it will not show you the conflicts, but it is a useful tool in merges.

当然,它不会向您显示冲突,但它是合并中的有用工具。