How to synchronize two git repositories How to synchronize two git repositories git git

How to synchronize two git repositories


Rather than making a bare clone, I prefer making a bundle (see "How can I email someone a git repository?"), which generates one file, easier to copy around (on an USB stick for instance)

The bonus is that is does have some of the characteristics of a bare repo: you can pull from it or clone it.
But only have to worry about one file.

machineB$ git clone /home/me/tmp/file.bundle R2

This will define a remote called "origin" in the resulting repository that lets you fetch and pull from the bundle. The $GIT_DIR/config file in R2 will have an entry like this:

[remote "origin"]    url = /home/me/tmp/file.bundle    fetch = refs/heads/*:refs/remotes/origin/*

To update the resulting mine.git repository, you can fetch or pull after replacing the bundle stored at /home/me/tmp/file.bundle with incremental updates.

After working some more in the original repository, you can create an incremental bundle to update the other repository:

machineA$ cd R1machineA$ git bundle create file.bundle lastR2bundle..mastermachineA$ git tag -f lastR2bundle master

You then transfer the bundle to the other machine to replace /home/me/tmp/file.bundle, and pull from it.

machineB$ cd R2machineB$ git pull


See this blog post "Synchronizing Git repositories without a server " (by Victor Costan).

This post describes a method for pushing changes between two repositories without using a server with network connections to both hosts having repositories

Start up by creating a repository on the USB stick.

mkdir /path/to/usb/stick/repository.gitgit clone --local --bare . /path/to/usb/stick/repository.git

Then register the repository on the USB stick as a remote repository, and push the desired branch to it (if you don't want to push master, substitute your desired branch).

git remote add usb file:///path/to/usb/stick/repository.gitgit push usb master

In the future, you can treat the USB repository as any other remote repository. Just make sure it's mounted :) For instance, the following pushes new changes to the USB repository.

git push usb

On the receiving end, mount the USB stick, and use a file URL for the repository

file:///path/to/usb/stick/repository.git

A few handy commands:

# cloning the repository on the USB stickgit clone file:///path/to/usb/stick/repository.git# updating a repository cloned from the USB stick using the above commandgit pull origin# adding the USB stick repository as a remote for an existing repositorygit remote add usb file:///path/to/usb/stick/repository.git# updating from a remote repository configured using the above commandgit pull usb master


Direct copy of a repository to the other file system is an alternative to bare clone or to bundle. After copying you can set the copied repo up directly as a local remote - unintuitive as local remote may seem - to fetch and merge into the first repository.

I.e. to merge repo2 from a second computer into ~/repo1, first copy repo2 to the repo1 file system at ~/repo2 (memory stick, network copy, etc.) and then you can use the answer to Git pulling changes between two local repositories:

~/repo1 $ git remote add repo2 ~/repo2~/repo1 $ git fetch repo2~/repo1 $ git merge repo2/foo

This works because as the wikipedia article on git says: "A Git repository — data and metadata — is completely contained within its directory, so a normal system-level copy (or rename, or delete) of an entire Git repository is a safe operation. The resulting copy is both independent of and unaware of the original."