Roland's homepage

My random knot in the Web

Merging local git repositories

For a long time, I’ve had a directory ~/templates to store template files for several programs.

For historical reasons, my LaTeX templates have been stored under ~/latex/templates.

Both have been under git revision control since 2006; before that I used rcs.

I want to merge both local repositories under ~/templates. This articla documents how that was done.

Preparations

Warning

This is where a little forethought goes a long way in preventing merge conflicts! Take care to move files in one or both repos (and commit those moves) to prevent having files with the same name in the same location in the merged repository.

In the ~/latex/templates, I made a latex subdirectory, moved everything to that and committed the move. Since the original ~/templates directory has never had a latex subdirectory, this ensures that there will be no merge conflicts.

Then I made a directory ~/merged-templates where the actual merge will take place.

Merge

The actual merge commands and their abbreviated output is shown below.

> cd ~/merged-templates
> git init .
Initialized empty Git repository in ~/merged-templates/.git/
> git remote add -f remote-1 ~/templates/
Updating remote-1
...
From ~/templates
* [new branch]      master     -> remote-1/master
> git remote add -f remote-2 ~/latex/templates/
Updating remote-2
...
From ~/latex/templates
* [new branch]      master     -> remote-2/master
> git merge remote-1/master --allow-unrelated-histories
> git merge remote-2/master --allow-unrelated-histories

Note that the --allow-unrelated-histories option to git merge is required for both merges. After this, the repositories are merged. You can check this with git log. All that remains is to do some cleanup.

Cleanup

First, I will remove the superfluous brances. This is done because these remotes will be either a self-reference (in the case of remote-1) or won’t exist in the future.

> cd ~/merged-templates
> git remote rm remote-1
> git remote rm remote-2

Next, I update the templates directory and remove the old latex template directory.

> cd
> mv templates old-templates
> mv merged-templates templates
> cd ~/latex
> rm -rf templates/

For comments, please send me an e-mail.


Related articles


←  Simple viewer for STEP files on UNIX Use pygmentize as a colorized cat  →