gc your git repositories automatically with a cron task

09 Dec 2011

I have a lot of git code repositories, and I usually gc (garbage collect) them manually by running the git gc command every now and then. Tasks like these are prime candidates for automating with cron. Below is a cron entry and the script which gcs my repositories. Hope you guys find it useful.

the script

 1 #!/bin/bash
 2 #author: Khaja Minhajuddin
 3 #email: minhajuddin.k@gmail.com
 4 #path /home/minhajuddin/.cron/reboot.sh
 5 #description: script which is executed everytime computer starts
 6 
 7 #git gc repos
 8 REPO_DIRS=$(cat <<EOS
 9 $HOME/repos
10 $HOME/repos/core
11 EOS
12 )
13 
14 for repo_dir in $REPO_DIRS
15 do
16   echo "checking for git repos in $repo_dir"
17   for repo in $(ls $repo_dir)
18   do
19     cd $repo_dir/$repo
20     if [[ -d .git ]]
21     then
22       echo "garbage collecting $repo"
23       git gc
24     fi
25   done
26 done

the crontab entry

1 $ crontab -e
2 #add the line below into the editor and save it
3 @reboot   $HOME/.cron/reboot.sh

Bonus tip: If you have a gitosis server, put the following script at ~git/.cron/reboot.sh and perform the above step for your git user.

the gitosis git user script

1 #!/bin/bash
2 
3 for repo in $(ls ~/repositories)
4 do
5   cd ~/repositories/$repo
6   echo "garbage collecting $repo"
7   git gc
8 done