是否有 git-merge --dry-run 选项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/501407/
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
Is there a git-merge --dry-run option?
提问by Otto
I'm merging in a remote branch that may have a lot of conflicts. How can I tell if it will have conflicts or not?
我正在合并一个可能有很多冲突的远程分支。我怎么知道它是否会发生冲突?
I don't see anything like a --dry-run
on git-merge
.
我没有看到任何像--dry-run
on git-merge
。
回答by mipadi
As noted previously, pass in the --no-commit
flag, but to avoid a fast-forward commit, also pass in --no-ff
, like so:
如前所述,传入--no-commit
标志,但为了避免快进提交,也传入--no-ff
,如下所示:
$ git merge --no-commit --no-ff $BRANCH
To examine the staged changes:
要检查分阶段更改:
$ git diff --cached
And you can undo the merge, even if it is a fast-forward merge:
您可以撤消合并,即使它是快进合并:
$ git merge --abort
回答by akostajti
I just had to implement a method that automatically finds conflicts between a repository and its remote. This solution does the merge in memory so it won't touch the index, nor the working tree. I think this is the safest possible way you can solve this problem. Here's how it works:
我只需要实现一种方法来自动查找存储库与其远程之间的冲突。此解决方案在内存中进行合并,因此它不会触及索引,也不会触及工作树。我认为这是解决此问题的最安全的方法。这是它的工作原理:
- Fetch the remote to your repository. For example:
git fetch origin master
- Run git merge-base:
git merge-base FETCH_HEAD master
- Run git merge-tree:
git merge-tree mergebase master FETCH_HEAD
(mergebaseis the hexadecimal id that merge-base printed in the previous step)
- 将远程获取到您的存储库。例如:
git fetch origin master
- 运行 git merge-base:
git merge-base FETCH_HEAD master
- 运行git merge-tree:
git merge-tree mergebase master FETCH_HEAD
(mergebase是上一步merge-base打印的十六进制id)
Now suppose that you want to merge the remote master with your local master, but you can use any branches. git merge-tree
will execute the merge in memory and print the result to the standard output. Grep for the pattern <<
or >>
. Or you can print the output to a file and check that. If you find a line starting with 'changed in both' then most probably there will be a conflict.
现在假设您想将远程 master 与本地 master 合并,但您可以使用任何分支。git merge-tree
将在内存中执行合并并将结果打印到标准输出。模式的 Grep<<
或>>
. 或者您可以将输出打印到文件并检查。如果您发现一行以 'changed in both' 开头,那么很可能会发生冲突。
回答by hades
My simple brute-force solution to this is:
我对此的简单蛮力解决方案是:
Create a "pre-master" branch (from master of course)
Merge all the things you want to into this pre-master.
Then you can see how the merging happened without touching master.- Merge pre-master into master OR
- Merge all wannabe-released branches into master
创建一个“pre-master”分支(当然来自master)
将您想要的所有内容合并到此预母版中。
然后你可以在不接触 master 的情况下看到合并是如何发生的。- 将 pre-master 合并到 master 或
- 将所有想要发布的分支合并到 master
Anyway, I would follow @orange80's advice.
无论如何,我会遵循@orange80 的建议。
回答by Brian Phillips
Undoing a merge with git is so easy you shouldn't even worry about the dry run:
撤消与 git 的合并非常简单,您甚至不必担心试运行:
$ git pull $REMOTE $BRANCH
# uh oh, that wasn't right
$ git reset --hard ORIG_HEAD
# all is right with the world
EDIT: As noted in the comments below, if you have changes in your working directory or staging area you'll probably want to stash them before doing the above (otherwise they will disappear following the git reset
above)
编辑:如以下评论中所述,如果您的工作目录或暂存区发生更改,您可能希望在执行上述操作之前将它们隐藏起来(否则它们将在执行上述操作后消失git reset
)
回答by Okonomiyaki3000
I made an alias for doing this and works like a charm, I do this:
我为此做了一个别名并且像魅力一样工作,我这样做:
git config --global alias.mergetest '!f(){ git merge --no-commit --no-ff ""; git merge --abort; echo "Merge aborted"; };f '
Now I just call
现在我只是打电话
git mergetest <branchname>
To find out if there are any conflicts.
找出是否有任何冲突。
回答by timh
Just diff your current branch against the remote branch, this will tell you what is going to change when you do a pull/merge.
只需将当前分支与远程分支进行比较,这将告诉您执行拉取/合并时会发生什么变化。
#see diff between current master and remote branch
git diff master origin/master
回答by ArnaudR
I use the request-pullgit command to do so. It allows you to see every change that would happen when merging, but without doing anything on your local or remote repositories.
我使用request-pullgit 命令来做到这一点。它允许您查看合并时会发生的每个更改,但无需在本地或远程存储库上执行任何操作。
For instance, imagine you want to merge a branch named "feature-x" into your master branch
例如,假设您想将一个名为“feature-x”的分支合并到您的主分支中
git request-pull master origin feature-x
will show you a summary of what would happen (without doing anything):
将向您展示将要发生的事情的摘要(不做任何事情):
The following changes since commit fc01dde318:
Layout updates (2015-06-25 11:00:47 +0200)
are available in the git repository at:
http://fakeurl.com/myrepo.git/ feature-x
for you to fetch changes up to 841d3b41ad:
----------------------------------------------------------------
john (2):
Adding some layout
Refactoring
ioserver.js | 8 +++---
package.json | 7 +++++-
server.js | 4 +--
layout/ldkdsd.js | 277 +++++++++++++++++++++++++++++++++++++
4 files changed, 289 insertions(+), 7 deletions(-)
create mode 100644 layout/ldkdsd.js
If you add the -p
parameter, you will also get the full patch text, exactly like if you were doing a git diff on every changed file.
如果您添加-p
参数,您还将获得完整的补丁文本,就像您对每个更改的文件执行 git diff 一样。
回答by Christoph B?hmwalder
I'm surprised nobody has suggested using patches yet.
我很惊讶还没有人建议使用补丁。
Say you'd like to test a merge from your_branch
into master
(I'm assuming you have master
checked out):
假设你想测试一个从your_branch
into合并master
(我假设你已经master
签出):
$ git diff master your_branch > your_branch.patch
$ git apply --check your_branch.patch
$ rm your_branch.patch
That should do the trick.
这应该够了吧。
If you get errors like
如果你得到这样的错误
error: patch failed: test.txt:1
error: test.txt: patch does not apply
that means that the patch wasn't successful and a merge would produce conflicts. No output means the patch is clean and you'd be able to easily merge the branch
这意味着补丁不成功,合并会产生冲突。没有输出意味着补丁是干净的,你可以轻松地合并分支
Note that this will notactually change your working tree (aside from creating the patch file of course, but you can safely delete that afterwards). From the git-apply documentation:
请注意,这实际上不会更改您的工作树(当然,除了创建补丁文件之外,您可以在之后安全地删除它)。从 git-apply 文档:
--check
Instead of applying the patch, see if the patch is applicable to the
current working tree and/or the index file and detects errors. Turns
off "apply".
Note to anyone who is smarter/more experienced with git than me: please do let me know if I'm wrong here and this method does show different behaviour than a regular merge. It seems strange that in the 8+ years that this question has existed noone would suggest this seemingly obvious solution.
请注意那些比我更聪明/更有经验的人:如果我在这里错了,请告诉我,这种方法确实显示出与常规合并不同的行为。奇怪的是,在这个问题存在的 8 多年里,没有人会提出这个看似显而易见的解决方案。
回答by Christoph B?hmwalder
This might be interesting: From the documentation:
这可能很有趣:来自文档:
If you tried a merge which resulted in complex conflicts and want to start over, you can recover with git merge --abort.
如果您尝试了导致复杂冲突的合并并想重新开始,您可以使用git merge --abort恢复。
But you could also do it the naive (but slow) way:
但是你也可以用天真的(但很慢)的方式来做到这一点:
rm -Rf /tmp/repository
cp -r repository /tmp/
cd /tmp/repository
git merge ...
...if successful, do the real merge. :)
(Note: It won't work just cloning to /tmp, you'd need a copy, in order to be sure that uncommitted changes will not conflict).
(注意:仅克隆到 /tmp 是行不通的,您需要一份副本,以确保未提交的更改不会发生冲突)。
回答by Jason McKindly
I am aware that this is an old question, but it is the first to appear on a Google search.
我知道这是一个老问题,但它是第一个出现在 Google 搜索中的问题。
Git introduced a --ff-only option when merging.
Git 在合并时引入了一个 --ff-only 选项。
From: http://git-scm.com/docs/git-merge
--ff-only
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.
来自:http: //git-scm.com/docs/git-merge
--ff-only
拒绝合并并以非零状态退出,除非当前 HEAD 已经是最新的或者合并可以解决为快进。
Doing this will attempt to merge and fast-forward, and if it can't it aborts and prompts you that the fast-forward could not be performed, but leaves your working branch untouched. If it can fast-forward, then it will perform the merge on your working branch. This option is also available on git pull
. Thus, you could do the following:
这样做将尝试合并和快进,如果不能,它会中止并提示您无法执行快进,但不会影响您的工作分支。如果它可以快进,那么它将在您的工作分支上执行合并。此选项也可用于git pull
。因此,您可以执行以下操作:
git pull --ff-only origin branchA #See if you can pull down and merge branchA
git merge --ff-only branchA branchB #See if you can merge branchA into branchB