Merge and Patch¶
- The two parents and their common base form the three stages of the
git show :1:fileis the
basewe have the difference as
git diff -1or
git diff --base
git show :2:fileis
ourswe have the difference as
git diff -2or
git diff --ours
git show :3:fileis
theirswe have the difference as
git diff -3or
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 ..otherbranchshow the changes that will be merged in the current branch.
git diff ...otherbranchis the diff from common ancestor (merge base) to graphical representation of the branches since they were merged last time.
git mergetoollaunch a graphical mergetool which will work you through the merge.
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
- Applying / Merging Changes From One Git Repository To Another
- git – applying patches
- Pro Git: Maintaining a Project
- gitready: pick out individual commits
git am, or with
git apply, and
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
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.