Want more powerful ways to leverage other projects using Git? Mirror a repository! Mirroring a repository involves creating an exact copy of the original repository, including all branches, tags, and commits. Create a mirror if you want to ‘fork’ a project from one git service to another or if you want to make a copy of a repository without attracting attention to yourself by publicly forking it. Or use a mirror to create a backup of one of your repos.
For example, I’ve seen companies create mirrors of OpenSource to ensure they keep access to projects no matter what happens to them. Instead of creating a fork, which makes a publicly visible link to their organization from the original project repo, they mirror the code to keep it on the DL. Companies don’t always host their code with popular services where public projects are likely to live, so clicking the “fork” button isn’t an option in any event.
This blog post will review the steps involved in mirroring a Git repository.
Step 1: Create a new empty repository
The first step in mirroring a Git repository is to create a new empty repository that will serve as the mirror. In other words, this repo is the target of your mirror. This repository can live anywhere – on Github, Gitlab, a private server, or anywhere.
Step 2: Clone the original repository
Next, clone the original repository using the Git clone command. This clone is a temporary copy. It’s an intermediary between the source and the destination. To do this, navigate to a directory where you want to clone the repository and run the following command:
git clone --mirror <original_repository_url>
This command will create a bare clone of the original repository in the current directory. The “–mirror” flag copies all branches and tags during the clone operation. Keep this repo around so you can use it to synchronize updates from the source to the destination at a later date.
Step 3: Push changes to the mirrored repository
Once you have cloned the original repository, you can push changes to the mirrored repository using the Git push command. To do this, navigate to the directory of your intermediary and run the following command:
git push --mirror <mirror_repository_url>
This command will push all branches, tags, and commits to the mirrored repository, creating an exact copy of the original repository.
Step 4: Keep the mirrored repository up to date
To ensure that the mirrored repository stays updated with the original repository, you should regularly pull changes from it and push them to the mirrored repository. To do this, navigate to the directory of the cloned repository and run the following command:
git fetch -p origin git push --mirror <mirror_repository_url>
The “git fetch -p origin” command will fetch all changes from the original repository, including new branches and tags. The “-p” flag removes remote-tracking branches that no longer exist in the original repository. The “git push –mirror” command pushes all changes to the mirrored repository.
Conclusion
Mirroring a Git repository can be a valuable way to fork a repo when it’s not as simple as pushing a button in GitHub. It enables you to work independently and control your destiny. Following the steps outlined in this blog post, you can easily create a mirrored repository and keep it up to date with the original one.