Mirroring Github repos on your own server

We have already discuss why Git is so useful in managing your source code. Now you may also want to use git to check out some interesting projects from other gurus on open source code sharing platforms, e.g. GitHub. Sometimes you may want to mirror the repo on your own server so you and your team can access it from there instead.

A local copy of the foreign repo that is always up-to-date

For example somehow you and your team need to pull the Facebook Android SDK┬áinto their workspace, instead of everyone pulling it from Github, you can clone the repo into a local server, and everyone just pull from the local server. This technique can reduce redundant network traffic and is particularly useful if your team don’t have a fast and reliable internet connection, or the connection to the remote repo is slow (GitHub could get very slow sometimes). Another benefit is that you can make sure everyone is getting the same version of source code because they are fetching from the same local copy.

The Basics

Basically we are using only 2 git commands, “git clone –mirror” and “git fetch”

Example

Environment
Assume you have a server called “server-dev” and your development machine can access the server by “server-dev.local”, clone all the repos you want to mirror in the server.

On you and your team’s development computers, you can clone the repo into their local workspace

Setup the cronjob to keep the repos fresh

After setting up all the stuffs above, you still need to call “git fetch” periodically on all of the repo dirs. Write a simple bash script to perform the “git fetch” command. Save the following content to /home/git/cronjobs/gitcronjob.sh

Cronjob setup:

Add the following line in the editor, this will tell the system to run the script once per hour (you may adjust the update frequency to your needs)

You’re done! Happy hacking!

Share your thoughts