可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
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.
Is there anyway to do this?
回答1:
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).
回答2:
The easiest way is to do git merge 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 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.
回答3:
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
回答4:
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
回答5:
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.
回答6:
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.