git Git交互式合并?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10935226/
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
Git Interactive Merge?
提问by Steven
I have two branches with the exact same file (incase you are wondering it is a .sql file) and I want to interactively merge it.
我有两个具有完全相同文件的分支(以防您想知道它是一个 .sql 文件),我想以交互方式合并它。
Pretty much I want to open up a diff program like I do when there is a conflict (or command line) and select exactly what lines go where.
我几乎想打开一个 diff 程序,就像我在发生冲突(或命令行)时所做的那样,并准确地选择哪些行去哪里。
Is there anyway to do this?
有没有办法做到这一点?
回答by Phil Miller
Yes, but it will mostly be by manually making that happen. You'll tell Git you're merging the two relevant branches, but that it shouldn't try to commit the result on its own, (edited to add:nor fast-forward if it thinks the merge is trivial):
是的,但主要是通过手动实现。你会告诉 Git 你正在合并两个相关的分支,但它不应该尝试自己提交结果,(编辑添加:如果它认为合并是微不足道的,也不会快进):
git merge --no-commit --no-ff branch-to-merge
Then you'll ask git for the file as it appeared in the two branches:
然后,您将向 git 询问出现在两个分支中的文件:
git show HEAD:filename >filename.HEAD
git show branch-to-merge:filename >filename.branch
and their merge base,
和他们的合并基础,
git show `git merge-base HEAD branch-to-merge`:filename >filename.base
You'll merge them using whatever tool you want (e.g.)
您将使用您想要的任何工具合并它们(例如)
meld filename.{HEAD,branch,base}
you'll stage that (git add filename
), and then commit the merge (git commit
).
您将暂存 ( git add filename
),然后提交合并 ( git commit
)。
回答by Brad O
The easiest way is to do git merge <other_branch
then git mergetool
to graphically resolve the conflicts. See #10935226for how to set up mergetool.
最简单的方法是git merge <other_branch
然后git mergetool
以图形方式解决冲突。有关如何设置合并工具,请参阅#10935226。
The hitch is, your changed file may fast-forward merge with with the older one. Then you have to get a bit more clever.
问题是,您更改的文件可能会与旧文件快速合并。然后你必须变得更聪明一点。
Novelocrat gives a great way to dig a bit deeper, but you will often have to change the initial command to git merge --no-commit --no-ff <other_branch>
because --no-commit really means "Don't commit the merge...unless it's a fast-forward merge." It's a bit of a stinger for lots of folks trying to do exactly what you want.
Novelocrat 提供了一种深入挖掘的好方法,但您经常需要将初始命令更改为,git merge --no-commit --no-ff <other_branch>
因为 --no-commit 真正意味着“不要提交合并......除非它是快进合并。” 对于许多试图完全按照您的意愿去做的人来说,这有点刺痛。
Sometimes the least confusing way is not very stylish: check out the other branch in a different working copy, use your favorite merge tool to get the version you want in the directory you want, then commit it.
有时最容易混淆的方式不是很时尚:在不同的工作副本中查看另一个分支,使用您最喜欢的合并工具在您想要的目录中获取您想要的版本,然后提交它。
回答by Hugh
As per this gist, where temp could be an existing branch.
根据此要点,其中 temp 可能是现有分支。
https://gist.github.com/katylava/564416
https://gist.github.com/katylava/564416
On master:
在主人上:
git checkout -b temp
On temp:
在温度:
git merge --no-commit --no-ff refactor
… which stages everything, so:
......这一切都分阶段,所以:
git reset HEAD
Then begin adding the pieces you want:
然后开始添加你想要的部分:
git add --interactive
回答by taj
From the branch you want to merge into:
从您要合并到的分支:
git checkout -p branch_to_merge --
This won't checkout the branch_to_merge, but will let you interactively add hunks from the patch (diff).
这不会检查 branch_to_merge,但会让你从补丁(差异)交互式地添加大块头。
回答by John Fisher
You could simply use WinMerge, DiffMerge, or any available diff/merge UI tool to do the work manually. If you want to hook it into "git difftool", you can search online to find ways to make those tools work with git.
您可以简单地使用WinMerge、DiffMerge或任何可用的 diff/merge UI 工具来手动完成这项工作。如果你想把它挂钩到“git difftool”中,你可以在线搜索找到让这些工具与 git 一起工作的方法。
回答by Dustin
The best way I have found to do this is:
我发现做到这一点的最好方法是:
- Checkout the branch with your changes
- Create a new branch from that point
- Reset your new branch to the commit you want to compare to and build upon. The reset will be a "mixed" reset by default, meaning that it will not change the "working tree", i.e. the actual code files
- At this point, my text editor (VSCode) shows me what is different between my current files and the commit I reset to. I can edit the code to select which lines I want to commit. What this does is allow me to see everything my branch changed and confirm every line of code I will commit. This is useful such as before merging my changes back into production.
- 使用您的更改签出分支
- 从该点创建一个新分支
- 将您的新分支重置为您想要比较和构建的提交。默认情况下重置将是“混合”重置,这意味着它不会改变“工作树”,即实际的代码文件
- 此时,我的文本编辑器 (VSCode) 向我展示了当前文件和我重置的提交之间的不同之处。我可以编辑代码以选择要提交的行。这样做的目的是让我看到我的分支更改的所有内容并确认我将提交的每一行代码。这很有用,例如在将我的更改合并回生产之前。