问题
I have a local branch that needs to be merged with the remote. Both remote and local branches have lots of different commits (on various files). Is it possible to identify and merge the files which have only fast-forward type (remote-side) changes? I would like to deal with all the other changes manually. git merge --ff-only does not merge anything when there is any two-sided changes.
Edit: I would like to make the question more clear. Let's say the original files (on parent node) are file1, file2, file3, file4. My local branch, I modified file1, file2, deleted file4, added file5. In their remote branch, they modified file1,file3, deleted file4, added file6. I would like to do the following:
- Identify all changes with the information of who made the change (with something like git diff): file1 (both sides) file2, file5 (my side) and file3 and file6 on their side.
- Merge only specific one-sided changes (from their side): After I merge my local with the remote, I should have file3 and file6 as modified in their remote branch and file1,file2,file5 as modified in my local branch. I will deal with file1 and file5 vs file6 manually.
回答1:
git read-tree is the low-level merge prep, everything short of actual conflict resolution. One easy way is
git merge
-s ours --no-commit
other
# no-op merge, just sets up parents
git read-tree -um $(
git merge-baseHEAD
other
) HEAD
other
#
manual resolution here
git commit
which leaves everything with different new content in both branches for manual resolution but accepts all at-most-one-new-version files. You might want read-tree
's --aggressive
option, to handle whole-file deletion and addition as an ordinary change.
As a safety play for an unlikely case, check the output of git merge-base--all HEAD
other
. If that shows multiple bases (as it will when the most recent merges on each branch have common parents), git's (default) "recursive" merge strategy would have derived base content that produces better automatic resolution results than the actual bases, and you might want to take extra care with your manual resolution.
回答2:
You could declare a merge driver similar to "Git - how to force merge conflict and manual merge on selected file".
That driver would use git merge-file, except you can test its exit value:
The exit value of this program is negative on error, and the number of conflicts otherwise.
If the merge was clean, the exit value is 0.
So if it returns 0, you can let the merge continue (your merge driver exits with status '0')
In any other cases, you will force a manual merge by exiting with a status '1'.
来源:https://stackoverflow.com/questions/24348953/is-it-possible-to-do-a-partial-merge-with-ff-only-changes