git clone --mirror

Posted on Thursday, September 11, 2014


I just finished reading the book a pragmatic guide to git.



In this book git clone --mirror command.   After looking it up at http://git-scm.com/docs/git-clone [1]


This is a way to clone a repository in such a way that it can be used as a remote repository.

I am still a novice to git. If I am getting part of this wrong, please call me out.







When I create a remote repository I usually do the following.

On the remote machine I create the directory for the repository and run the following


    >  git init --bare


This creates the empty repository that can be used as a remote repository.


Then I push up a local repository to it.


This works great!




What about a second remote repository?


Currently I have all my remote repositories set up and working like I want.  I do want to set up a second, off-site, location for my remote repositories.   Prior to reading this book I had gone into my off-site, AWS virtual server, and running



    >  git init --bare



Then I would push up to this second remote.


With the --mirror command you can cut out the middle man.

From my off-site server I set up the location to clone a repo to and then ran the clone command, but with the --mirror option


    >  git clone --mirror git@git.example.com:/git/myrepo.git myrepo.git


Replace the location with your own.






Handling this "backup" remote repo

  
Now that I have two git remote repositories how do I handle keeping them in sync?


I could of course push updates to both repositories, but I imagine I will forget to do that sometimes, since the second repo is just a backup.

I could push to the backup repo once a month, or even year.

Or I could run this command from the remote repository


    >  git remote update


This will update the remote from the original remote repository it was cloned from.  Also it seems like a cleaner solutions for me, since my intention for these secondary repos is merely an off-site backup.




Simple Script


All my repos are in one directory so I made the following simple script to update them all in one go.



    >  sudo vi updateallremotes.sh




#!/bin/bash

for folder in `find $PWD -maxdepth 1 -mindepth 1 -type d`; do
  echo $folder
  pushd .
  cd $folder
  git remote update
  popd
done



It simply runs the "git remote update" command in every folder one level deep.

Make it executable


    >  chmod u+x  updateallremotes.sh



Then run it


    >  ./updateallremotes.sh








Adding it to cron, run nightly



    >  sudo vi /etc/crontab



I put the following in it to run this command once a week (update the command with the full path)


# minute hour     mday    month     wday      who   command

22        2       *       *         0         git  cd /git && ./updateallremotes.sh


            -Issue this command at 2:22 AM every Sunday and its run as the git user on my system.






References
[1]  git-clone Manual Page
       Visited 9/2014



No comments:

Post a Comment