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.
Basically we are using only 2 git commands, “git clone –mirror” and “git fetch”
# to initialize a gir bare repo which is the exact copy of the remote repo
git clone --mirror https://github.com/jquery/jquery.git
# to pull updates from upstream
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.
(Login to server-dev as user "git")
git@server-dev $ mkdir mirrors
git@server-dev $ cd mirrors
git@server-dev $ git clone --mirror https://github.com/facebook/facebook-android-sdk.git
git@server-dev $ git clone --mirror https://github.com/facebook/facebook-php-sdk.git
git@server-dev $ git clone --mirror https://github.com/jquery/jquery.git
On you and your team’s development computers, you can clone the repo into their local workspace
jason@pc1 $ git clone firstname.lastname@example.org:facebook-android-sdk.git
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
#This script iterates all Git repos and update it automatically
#This keep the Repos always up to date
for file in /home/git/mirrors/*
git@server-dev $ crontab -e
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)
0 * * * * /home/git/cronjobs/gitmirror.sh
You’re done! Happy hacking!