0

Update all your git repos at once

Welcome to post 3 of my 100 day challenge. Checkout my introduction for some background.

This is a short one today. Due to time constraints and workload I’ve not been able to expand this post as much as I would like to.

A lot of us tend to have many git repos checked out from GitHub or another central remote repository and have a need to pull from that remote master periodically to keep those repos up to date.

At work I can be using 4 or more git repos which could be being updated at any time by other people. To ensure that I have the most up to date code I wrote a little helper script which will pull each one in turn. I am aware that this functionality can be achieved with git hooks. I believe you can even set it up so that when someone pushes to the remote you can get it to update remote(s) elsewhere as well but the script I’m about to share provides enough functionality for me at the moment.

I’ve provided the script below in case it is of any use to you, it will update all your git repos at once. I make an assumption that your repos live in /opt/git-repos but you can change this yourself if that is not the case:

#!/bin/bash
 
repo_list=( '<repo 1>' '<repo 2>' '<repo 3>' '<repo 4>' )
 
for repo in "${repo_list[@]}"
do
  echo "Updating ${repo}"
  cd /opt/git-repos/$repo
  git pull
done

As I said it is super simple but achieves what I want. I usually run this script first thing in the morning and periodically during the day to ensure that I have any changes.

I intend to write a follow up with an expanded version of this script that takes the repos to update as an array of command line arguments and explains how it can then be run via cron if required. Keep checking back here to see that post when it is published.

Subscribe to my feed either by E-mail or by RSS to receive updates as they happen.

Can you improve on any of the tips I’ve discussed here? If you can let me know in the comments.

Jason Edwards