Merge and Patch

Merging

  • The two parents and their common base form the three stages of the merge:
    • git show :1:file is the base we have the difference as git diff -1 or git diff --base
    • git show :2:file is ours we have the difference as git diff -2 or git diff --ours
    • git show :3:file is theirs we have the difference as git diff -3 or git diff --theirs
  • In a failed merge an unmerged path file contains the combined unmerged file, and git diff will show a combined diff which show differences with the two parents.
  • git log --merge -p <path> will show diffs first for the HEAD version and then the MERGE_HEAD version.
  • git log ..otherbranch show the changes that will be merged in the current branch.
  • git diff ...otherbranch is the diff from common ancestor (merge base) to graphical representation of the branches since they were merged last time.
  • git mergetool launch a graphical mergetool which will work you through the merge.

Keeping one branch

If in your repo you have a conflict in a file, but want to adopt either ours or theirs version of the file, you don’t need to manually edit the merge, you can checkout the chosen file:

$ git checkout --theirs <file>
$ git add <file>

Refs:

Submitting patches

Use git format-patch

$ git format-patch origin

Or with a commit range:

$ git format-patch before..end

Produces in the current directory a sequence of patches, with names from each commit log.

You can apply them one by one or as a whole with:

$ git am patches.mbox

git-am will fail if the patch does not apply, you can instead use a 3 ways merge with:

$ git am -3 patches.mbox

You can fix conflicts, add the fixed files to the index and commit with:

$ git am --resolved
Refs:

Subtree-merge

Subtree merge is a very useful strategy to import a subtree from an other repository in a branch of our repository.

It is presented in a very concise way in Git howto: How to use the subtree merge strategy from which I extract the following code that illustrates the merging of a project B in the subdirectory dir-B of our project.

$ git remote add -f Bproject /path/to/B
$ git merge -s ours --no-commit Bproject/master
$ git read-tree --prefix=dir-B/ -u Bproject/master
$ git commit -m "Merge B project as our subdirectory"

To follow the changes in the B project you use:

$ git pull -s subtree Bproject master

This strategy ia applied to a bigger example in GitHub Help: Working with subtree merge

A different technique, doing the merge after the read-tree is in Scott Chacon Pro Git: Subtree Merging and used in this Git Subtree Workflow by David S Anderson and in Git Subtree Merge –The Quick Version by John Atten.