Can a local Git clone be considered a complete backup of the repo it was cloned from?

Git

Git Problem Overview


Suppose I have cloned a Git repository to my local disk using:

git clone username@git.example.com:someproject.git

Now suppose that git.example.com is not being backed up, and it goes down in a blaze of glory. Does my clone contain everything necessary to rebuild the remote repo that was lost? The Ultimate Backups section of Git Magic suggests that the answer is "yes," but it isn't clear to me.

Note that I'm not asking "is my local clone a sufficient backup of the master branch?" I'm asking whether my local clone can be considered a complete backup of everything that was contained in the remote repo; all branches, all tags, everything. For example, what about remote branches that aren't tracked in the local repo?

To futher confuse the issue, the existence of git clone --mirror suggests to me that my local clone should not be considered a complete backup of the remote repo.

Git Solutions


Solution 1 - Git

A clone can be considered a full backup of all the data in your remote repository, but not necessarily the meta-data (that's where the --mirror switch comes in). Your clone will contain all the commit, tree, blob, branch, and tag objects that are in any way referenced by the repository. That means your backup will contain all your source code, history, and associated branches or tags.

The difference with the --mirror switch is that without it, the clone won't include things like remotes that have been created on the server. These are not important in a "I hope I haven't lost any source!" kind of way, but they may be for getting your server back up and running like it was.

If you're interested in creating a backup that can be restored onto the server like there was never any issue, then you should use --mirror, but for most scenarios a simple clone is fine.

Solution 2 - Git

IMPORTANT

Without --mirror, The clone will not be a complete backup. Any line of work not visible in git branch -r will be elided from the clone.

Simple Demo

Witness a simple repo.

$ git init G
$ cd G
$ for f in 1 2 3 4; do date >1 && git add 1 && git commit -m $f; sleep 1.1; done
$ git log --oneline --graph --all --decorate
* 3c111bd (HEAD -> master) 4
* a08fea4 3
* d5c8d73 2
* 802856b 1

Add a branch:

$ git checkout d5c8d73
HEAD is now at d5c8d73... 2
$ git branch starts-at-2
$ git checkout starts-at-2
Switched to branch 'starts-at-2'
$ for f in 1 2 3 4; do date >1 && git add 1 && git commit -m 2-$f; sleep 1.1; done
$ git log --oneline --graph --all --decorate
* 6bb05bf (HEAD -> starts-at-2) 2-4
* fe1b635 2-3
* a9323fb 2-2
* 33502af 2-1
| * 3c111bd (master) 4
| * a08fea4 3
|/
* d5c8d73 2
* 802856b 1

Clone the repo.

$ cd ..
$
$ git clone G G2
Cloning into 'G2'...
$ cd G2
$ git log --oneline --graph --all --decorate
* 6bb05bf (HEAD -> starts-at-2, origin/starts-at-2, origin/HEAD) 2-4
* fe1b635 2-3
* a9323fb 2-2
* 33502af 2-1
| * 3c111bd (origin/master) 4
| * a08fea4 3
|/
* d5c8d73 2
* 802856b 1

Fine. Clone again.

$ cd ..
$ git clone G2 G3
$ cd G3
$ git log --oneline --graph --all --decorate
* 6bb05bf (HEAD -> starts-at-2, origin/starts-at-2, origin/HEAD) 2-4
* fe1b635 2-3
* a9323fb 2-2
* 33502af 2-1
* d5c8d73 2
* 802856b 1

Urk.

Solution 3 - Git

Your local clone won't be a complete backup. It will be a backup of the state of that repository, but it won't have all the refs of the source repository (so it won't know about the state of any remote branches).

For a complete backup, you correctly found git clone --mirror. This will not only have the branches for the original repository. It will also map all refs including remote branches.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMattView Question on Stackoverflow
Solution 1 - GitJames GregoryView Answer on Stackoverflow
Solution 2 - GitbobbogoView Answer on Stackoverflow
Solution 3 - GitAbizernView Answer on Stackoverflow