If I want to replicate a repo, I use this.
git clone --mirror $SRC $DST
But this command requires no repo exist at $DST. It's generally okay but if I want to issue Git commands concurrently from multiple processes, it may cause some race condition as no lockable file object exists before executing the command.
If I just want to skip existence test for $DST, I think I can do
mkdir -p $DST
cd $DST
git init --bare
git remote add origin --mirror-=fetch $SRC 2>/dev/null || true
git remote update
Would this make exactly same repo with git clone --mirror $SRC $DST?
If you look at the test done for a
git clone --mirror, you would see a mirror clone is one with:fetchrefspec set to+refs/*:refs/*git config --bool remote.origin.mirrorset to trueIf those conditions are met after your git remote commands, then yes, that would be equivalent to a mirror clone.
Running
git remote add --mirror=fetch && git fetch --allafter initializing a bare repository is almost the same asgit clone --mirror.But not quite.
When you use
git remote add --mirror=fetch, Git configures the fetch refspec to+refs/*:refs/*, which does indeed mirror the behavior ofgit clone --mirrorin terms of fetching all branches, tags, and other references from the source repository. And by executinggit fetch --allfollowing the remote addition, you are instructing Git to fetch from all remotes, which, in this context, means fetching all data from the source repository you have just added.git clone --mirrorautomatically sets the configurationremote.<remote>.mirrortotrue. That configuration is essential for the repository to be treated as a true mirror, affecting not just fetch operations but also push behaviors. It makes sure the repository can serve as a complete mirror for both fetching and pushing changes.That means that, while
git remote add --mirror=fetch && git fetch --allsuccessfully mirrors the content of the source repository into your local repository, to achieve complete equivalence withgit clone --mirror, you need to manually set theremote.<remote>.mirrorconfiguration: