Would `git remote add --mirror=fetch` make same repo with `git clone --mirror`?

2.1k views Asked by At

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?

1

There are 1 answers

2
VonC On BEST ANSWER

If you look at the test done for a git clone --mirror, you would see a mirror clone is one with:

  • a fetch refspec set to +refs/*:refs/*
  • a git config --bool remote.origin.mirror set to true

If those conditions are met after your git remote commands, then yes, that would be equivalent to a mirror clone.


So, if I run git remote add --mirror=fetch && git fetch --all, is it the same with "git clone --mirror"?

Running git remote add --mirror=fetch && git fetch --all after initializing a bare repository is almost the same as git 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 of git clone --mirror in terms of fetching all branches, tags, and other references from the source repository. And by executing git fetch --all following 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 --mirror automatically sets the configuration remote.<remote>.mirror to true. 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 --all successfully mirrors the content of the source repository into your local repository, to achieve complete equivalence with git clone --mirror, you need to manually set the remote.<remote>.mirror configuration:

git config --bool remote.origin.mirror true