BlogCode

An easy way to backup your GitHub repositories (for free)

Written by Codemzy on October 12th, 2023

As I use a cloud IDE, sometimes the only copy of my code is in a GitHub repo. And that got me worried. Here's how I create a backup on Bitbucket.

I pretty much do all of my coding in the cloud*, especially my full-stack development - even as a solo coder.

*For full-stack development in the cloud, I use Gitpod. I'm a massive fan of how easy it was to get started (with VS Code), it's been super reliable for years, and the support has always been excellent.

I don't need to code with a team, or make use of collaboration features (it's a lonely world here at my desk!) - but I do like being able to quickly spin up my dev environment on any of my devices. I also like not having node modules downloaded on my computer!

So I don't have a local copy of node modules. But I don't have a local copy of my code either.

And while I sometimes have a copy of a repo on GitPod, that's only if I'm actively working on it.

That means the only copy is on GitHub.

GitHub has been around since 2008 and was acquired by Microsoft in 2018 - so I trust that GitHub isn't going to disappear overnight.

But I don't like only having one copy of my code.

What if GitHub has some downtime and I need to fix a bug? Or what if there's some catastrophe at GitHub HQ? Or (probably more likely), I somehow get locked out of my account?

So after this genuinely kept me up one night, I decided I needed a backup solution. Maybe not for that recipe app I never launched, or the word clock I built 5 years ago but never use (that I still think is pretty cool). But at least for my more important projects.

word clock project

So how can you create a backup of your GitHub repos?

Push to another git hosting platform

GitHub is probably the biggest and most well-known git hosting platform, but there are others available. Bitbucket and GitLab are two pretty popular alternatives.

But you don't have to just host your git repo on GitHub, you could also host it on another provider.

As you probably know, pushing your code changes to GitHub is super easy - you just git push.

And it's just as easy to git push to another origin.

This means you can have your repo hosted in two places. Or three. I feel like two is enough since I imagine GitHub and its alternatives have their own backups in place.

Once you have this setup, instead of git push, you can use git push && git push backup or npm run git-push to save your repo in two places instead of one!

Here's how my quick and easy way to backup your GitHub repositories (for free) works:

Create a new blank repo

I start by creating a new blank repo on an alternative platform first. For this example, I'll use Bitbucket.

So I log in and click Create > Repository.

And I name it the same as on GitHub so I know what it is.

Add it as a new remote

Now I have a new repository that I can add as a new remote to my git repo.

I'm not going to add it as origin, since that's GitHub. But I'll add it as backup or bitbucket.

git remote add backup https://<your_username>@bitbucket.org/<workspace_ID>/<repo_name>.git

Make sure your repo is up to date with GitHub

If for whatever reason, your remote GitHub repo is ahead, you can run git fetch origin to get the latest changes.

Push it all to your backup remote

Now it's time for the magic. I'll push my code to the new backup remote.

git push --all bitbucket && git push --tags bitbucket

Now I have the repo backed up I can run git push && git push backup when I make any further commits to keep the backup always up-to-date.

Or I can add a script...

Add a script to push to both the origin and the backup destinations

For example in Node.js, you can add a script for backup:

{
  "scripts": {
    "backup": "git fetch --prune origin && git push --all backup && git push --tags backup",
  },
}

When you finish making changes to your code, after you run the git push command, you can do npm run backup to also push your code to the backup repository.

Or so you never forget, a script for git-push:

{
  "scripts": {
    "git-push": "git push origin && git push backup",
  },
}

With the git-push script, instead of git push origin you would do npm run git-push to push your code to both your origin and the backup platform.


Need a different solution? Here are some other of the options I considered*:

*I'm including details on these because if you don't like my solution you might want to pick one of these alternative methods instead.

Download the repo from the GitHub website

If you want a local copy of your repo to backup, you can download it from the GitHub website.

Navigate to your repository and then Code > Download ZIP.

GitHub download zip menu

This works pretty well for projects I've finished or I'm not actively working on, but I'd need to remember to do it every time I make a change.

I think this method downloads all the branches (and not just the one you are on in GitHub).

The positive is, that once I've downloaded the repo, I have a local copy, and my computer is also backed up, so I then get two extra copies!

But the negative is, that for projects I'm working on, I need to remember to manually* download the latest version each time I make a change. And then delete the older version I don't need anymore. That's going to get a little tedious.

And even if I remember to do it, the copy is only on my main device, and if I'm away I can't access it.

*If you did want a local copy, I found a couple of projects like GitHub-Backup and python-github-backup to make things a little less manual.

Use a backup service

There are a few services that seem to offer a GitHub backup-as-a-service.

But this backup isn't free like my original solution above. It involves spending money. Not always a lot of money - some start at $14 a month for unlimited repositories. If you don't like the solution I settled on, this might be an option worth considering.

Here are a couple of GitHub backup services I found from a quick Google search:

These services are in no particular order, and I've not used either of these services, so please do your own research (or a free trial) before committing to anything.