When migrating or managing Git repositories a valuable process I have learned is to Mirror a Git repository. Mirroring a Git repository will copy and overwrite a local source repository to a destination repository. In this article I will show how to perform this function using Powershell and using GitBash.
How to Mirror a local repository to a Git host provider
The following g assumes that you have already have access setup to the destination git host provider like Azure DevOps, GitHub or BitBucket. the following also assumes that you have cloned everything you want to copy from a source Git host provider.
With Powershell open:
- Navigate to the git repository directory input cd and the directory path where the git repository resides.
> cd C:\repos\my-source-repo
- Enter the git push command with the mirror flag and the destination git repository url.
> C:\repos\my-source-repo> git push —-mirror https://destinationgithostname/gitrepopath/my-source-repo.git
What if I don’t have the repository on my computer?
This would only require an additional step assuming, that you have already setup access to the source server.
With Powershell open
- Navigate to the folder on your computer where you want to clone the repository to
> cd C:\repos
- Next you need to clone the repository down to your computer
> git clone —-bare https://sourcegithostname/username/my-repo-name.git
- You will need to navigate to your new directory you can input dir then hit enter to verify it was created or you can input the following.
> cd C:\repos\my-repo-name
- Finally you can enter the git push command with the mirror flag and the destination git repository url.
> git push —-mirror https://destinationgithostname/gitrepopath/my-source-repo.git
Conclusion
That is all that it takes to Mirror a git repository to a new one. I hope you find this useful.