What's the difference between git clone --mirror and git clone --bare
The git clone help page has this to say about --mirror
:
Set up a mirror of the remote repository. This implies
--bare
.
But doesn't go into detail about how the --mirror
clone is different from a --bare
clone.
The difference is that when using --mirror
, all refs are copied as-is. This means everything: remote-tracking branches, notes, refs/originals/* (backups from filter-branch). The cloned repo has it all. It's also set up so that a remote update will re-fetch everything from the origin (overwriting the copied refs). The idea is really to mirror the repository, to have a total copy, so that you could for example host your central repo in multiple places, or back it up. Think of just straight-up copying the repo, except in a much more elegant git way.
The new documentation pretty much says all this:
--mirror
Set up a mirror of the source repository. This implies
--bare
. Compared to--bare
,--mirror
not only maps local branches of the source to local branches of the target, it maps all refs (including remote branches, notes etc.) and sets up a refspec configuration such that all these refs are overwritten by agit remote update
in the target repository.
My original answer also noted the differences between a bare clone and a normal (non-bare) clone - the non-bare clone sets up remote tracking branches, only creating a local branch for HEAD
, while the bare clone copies the branches directly.
Suppose origin has a few branches (master (HEAD)
, next
, pu
, and maint
), some tags (v1
, v2
, v3
), some remote branches (devA/master
, devB/master
), and some other refs (refs/foo/bar
, refs/foo/baz
, which might be notes, stashes, other devs' namespaces, who knows).
git clone origin-url
(non-bare): You will get all of the tags copied, a local branchmaster (HEAD)
tracking a remote branchorigin/master
, and remote branchesorigin/next
,origin/pu
, andorigin/maint
. The tracking branches are set up so that if you do something likegit fetch origin
, they'll be fetched as you expect. Any remote branches (in the cloned remote) and other refs are completely ignored.git clone --bare origin-url
: You will get all of the tags copied, local branchesmaster (HEAD)
,next
,pu
, andmaint
, no remote tracking branches. That is, all branches are copied as is, and it's set up completely independent, with no expectation of fetching again. Any remote branches (in the cloned remote) and other refs are completely ignored.git clone --mirror origin-url
: Every last one of those refs will be copied as-is. You'll get all the tags, local branchesmaster (HEAD)
,next
,pu
, andmaint
, remote branchesdevA/master
anddevB/master
, other refsrefs/foo/bar
andrefs/foo/baz
. Everything is exactly as it was in the cloned remote. Remote tracking is set up so that if you rungit remote update
all refs will be overwritten from origin, as if you'd just deleted the mirror and recloned it. As the docs originally said, it's a mirror. It's supposed to be a functionally identical copy, interchangeable with the original.
$ git clone --mirror $URL
is a short-hand for
$ git clone --bare $URL
$ (cd $(basename $URL) && git remote add --mirror=fetch origin $URL)
(Copied directly from here)
How the current man-page puts it:
Compared to
--bare
,--mirror
not only maps local branches of the source to local branches of the target, it maps all refs (including remote branches, notes etc.) and sets up a refspec configuration such that all these refs are overwritten by agit remote update
in the target repository.
My tests with git-2.0.0 today indicate that the --mirror option does not copy hooks, the config file, the description file, the info/exclude file, and at least in my test case a few refs (which I don't understand.) I would not call it a "functionally identical copy, interchangeable with the original."
-bash-3.2$ git --version
git version 2.0.0
-bash-3.2$ git clone --mirror /git/hooks
Cloning into bare repository 'hooks.git'...
done.
-bash-3.2$ diff --brief -r /git/hooks.git hooks.git
Files /git/hooks.git/config and hooks.git/config differ
Files /git/hooks.git/description and hooks.git/description differ
...
Only in hooks.git/hooks: applypatch-msg.sample
...
Only in /git/hooks.git/hooks: post-receive
...
Files /git/hooks.git/info/exclude and hooks.git/info/exclude differ
...
Files /git/hooks.git/packed-refs and hooks.git/packed-refs differ
Only in /git/hooks.git/refs/heads: fake_branch
Only in /git/hooks.git/refs/heads: master
Only in /git/hooks.git/refs: meta
I add a picture, show config
difference between mirror and bare.
The left is bare, right is mirror. You can be clear, mirror's config file have fetch
key, which means you can update it,by git remote update
or git fetch --all