Openhood

Mirror a GIT Repository for Backup

Image by by Edi Libedinsky on unsplash

Here’s a quick way to easily maintain a complete mirror of a git repository. This is especially useful for backup.

go to your repository

cd my_project

check your existing remote

git remote -v
(out)origin <git@mydomain.tld>:my_project.git (fetch)
(out)origin <git@mydomain.tld>:my_project.git (push)

Add a new remote, a github.com private repository for example, the --mirror flag is what’s different from a simple new remote

git remote add --mirror <git@github.com>:Openhood/my_project.git

Check the new remote

git remote -v
(out)github <git@github.com>:Openhood/my_project.git (fetch)
(out)github <git@github.com>:Openhood/my_project.git (push)
(out)origin <git@mydomain.tld>:my_project.git (fetch)
(out)origin <git@mydomain.tld>:my_project.git (push)

To discover the difference check your .git/config, the new remote has the config mirror = true

cat .git/config
(out)... file start skipped
(out)[remote "github"]
(out)url = <git@github.com>:Openhood/my_project.git
(out)fetch = +refs/*:refs/*
(out)mirror = true

Now all you have to do to mirror your entire repository is

git push github
Copyright © 2024 Openhood SARL