Automatically Pulling Multiple Repos From GitHub on Mac
I have a number of GitHub repositories on my Mac and I wanted a way to auto-pull them on a regular basis. Here's how I did it.
Any regular readers of my waffle will know that I'm not the sharpest tool in the box. Git is a core part of my workflow for a number of my projects, but being the sausage that I am, I often forget to pull the latest changes from GitHub before doing some work locally. Git then inevitably gets itself confused and it becomes a whole thing to resolve the conflict(s).
So I needed a way to bypass my sausageness, by automatically pulling from all my repositories on a regular basis. I ended up doing this in 2 steps:
- Creating a way to pull from all repos in one go
- Automating that pull
Pulling from GitHub
My first instinct was to build a shell script, then run that script regularly with a cron. All very simple, and something like this would have worked:
#!/bin/sh
cd ~/GitHub/
cd 100daystooffload.com/
git pull
cd ..
cd 512kb.club/
git pull
cd ..
cd kevquirk.com/
git pull
cd ..
You get the idea. The problem with this solution is that I would need to update the script every time I added or removed a repository.
I'm too lazy for that shit, so I hit up Kagi to see if I there was a better option out there.
There was.
find ./GitHub -mindepth 1 -maxdepth 1 -type d -print -exec git -C {} pull \;
Honestly, I don't really understand what this command is doing in its entirety, but from what I can gather, it's looking in my ./GitHub
folder (which is where all my repos live), then going a maximum of 1 folder level deep, and doing a git pull
.
It's not perfect, as it tries to pull from every folder within ./GitHub
, some of which aren't git repositories. But that's fine, it just skips over them.
While I was there, I went ahead and added an alias to my .zshrc
file so I can run the command quickly on demand:
nano .zshrc
alias pullall="find ./GitHub -mindepth 1 -maxdepth 1 -type d -print -exec git -C {} pull \;"
Now when I run the pullall
command from my terminal window, my Mac will pull everything. Nice.
Automating things
Ok, so now that I've solved the pulling issue, I want to automate it. I could have gone with Mac Automator, but to be honest, that thing confuses the shit out of me. Instead, I went with good old crontab
.
So I opened up my terminal and entered crontab -e
only to be faced with a Vim window. Yeah, screw that; I'm a Nano guy. So first I had to fix that.
Another quick Kagi search later, I added this to my .zshrc
file to force Nano as my default editor in terminal:
# Set default file editor to nano
export EDITOR=nano
export VISUAL="$EDITOR"
I restarted my terminal window (so .zshrc
was reloaded) and tried crontab -e
again. Huzzah! It loaded in Nano and I added a cronjob for my pull command that runs every 2 mins:
# Pulls git repos every 2 mins
*/2 * * * * find ./GitHub -mindepth 1 -maxdepth 1 -type d -print -exec git -C {} pull \;
Testing it worked
Final thing to do was to make a change on the live website, push it, and wait. A minute or so later the changes silently appeared in my local repo, so everything is working as expected. Niiiiice.
So there you go, a quick guide on how to automate pulling multiple Git repositories on a Mac. All very basic and easy to find online, but thought I'd combine it into a single post, more for my use that anything else.
Want more?
So you've read this post and you're still not satisfied? Ok then, here's some other stuff for you to do: