Merge subdirectory from another git repository

Let’s say I have two repositories: cvhci-standards and labeltracks.




I’d like to merge the content of subdirectory python from cvhci-standards into labelstracks/utilities/cvhcistandards. To do this, we use git read-tree, with the following steps:

1. Add remote repository and fetch it:

git remote add -f cvhci-standards git://

2. Merge, but don’t commit just yet:

git merge -s ours --no-commit cvhci-standards/master

3. Read a specific subdirectory from cvhci-standards. We specify the the target subdirectory as prefix, and the source-subdirectory (python) after the colon in the tree-ish repository description.

git read-tree --prefix=utilities/cvhcistandards/ -u cvhci-standards/master:python

4. Commit

git commit -m 'merging in cvhci-standards python module'

5. Pull from cvhci-standards to update the subtree.

git pull -s subtree -X subtree=utilities/cvhcistandards cvhci-standards master

We need to specify the subdirectory to merge to, otherwise git can’t match the subtrees correctly.


Git: make existing branch track remote branch

If you have an existing branch, say master, that was not cloned from a remote repository, a git pull without further arguments will give you the following error message:

You asked me to pull without telling me which branch you
want to merge with, and 'branch.master.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull ').
See git-pull(1) for details.

If you often merge with the same branch, you may want to
use something like the following in your configuration file:
[branch "master"]
remote =
merge =

[remote ""]
url =
fetch =

See git-config(1) for details.

Editing .git/config by hand is cumbersome, and I never can remember what the “syntax” for is.

As of git 1.7.0, it is now really easy to set this up:

git branch --set-upstream master origin/master

which will put the following into your .git/config

[branch "master"]
remote = origin
merge = refs/heads/master

Now you can git pull and the remote changes in origin/master will be automatically merged into your local master branch.