Git 互动合并 Git Interactive Merge? ?

我有两个分支具有完全相同的文件(如果你想知道它是一个。Sql 文件) ,我想交互式地合并它。

我非常想打开一个 diff 程序,就像我在存在冲突(或命令行)时所做的那样,并精确地选择哪些行到哪里。

还有别的办法吗?

76869 次浏览

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 merge --no-commit --no-ff branch-to-merge

Then you'll ask git for the file as it appeared in the two branches:

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).

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.

The easiest way is to do git merge <other_branch then git mergetool to graphically resolve the conflicts. See #10935226 for how to set up mergetool.

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.

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.

As per this gist, where temp could be an existing branch.

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

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).

http://git-scm.com/docs/git-checkout

The best way I have found to do this is:

  1. Checkout the branch with your changes
  2. Create a new branch from that point
  3. 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
  4. 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.