Reputation: 5751
Is there a simple way to backup an entire git repo including all branches and tags?
Upvotes: 199
Views: 193348
Reputation: 1330092
git bundle
I like that method, as it results in only one file, easier to copy around.
See ProGit: little bundle of joy.
See also "How can I email someone a git repository?", where the command
git bundle create /tmp/foo-all --all
is detailed:
git bundle
will only package references that are shown bygit show-ref
: this includes heads, tags, and remote heads.
It is very important that the basis used be held by the destination.
It is okay to err on the side of caution, causing the bundle file to contain objects already in the destination, as these are ignored when unpacking at the destination.
To use that bundle, you can clone it, specifying a non-existent folder (outside any git repo):
git clone /tmp/foo-all newFolder
As noted by shayan in the comments, git bundle
won't include stashed commits, even if git show-ref
can technically show a stashed commit.
As explained in "git bundle
stashed code", by torek:
anything that is not an actual reference name falls afoul of this problem, including reflog references like
stash@{1}
.
(You can try git show-ref stash@{1}
: it returns nothing)
Git 2.48 (Q1 2025), batch 12, adds documentation for "git bundle
"(man), and provide an example about how to make a “full backup” (git bundle create backup.bundle --all
) with caveats about what that means in this case, vs. transferring the history of a repository to another machine when the two machines have no direct connection: see examples.
Upvotes: 272
Reputation: 611
Expanding on the great answers by KingCrunch and VonC
I combined them both:
git clone --mirror [email protected]/reponame reponame.git
cd reponame.git
git bundle create reponame.bundle --all
After that you have a file called reponame.bundle
that can be easily copied around. You can then create a new normal git repository from that using git clone reponame.bundle reponame
.
Note that git bundle
only copies commits that lead to some reference (branch or tag) in the repository. So dangling commits are not stored to the bundle.
Upvotes: 50
Reputation: 21
There is a very simple to use python tool that automatically backs up organisations' repositories in .zip format by saving public and private repositories and all their branches. It works with the Github API : https://github.com/BuyWithCrypto/OneGitBackup
Upvotes: 1
Reputation: 361
This thread was very helpful to get some insights how backups of git repos could be done. I think it still lacks some hints, information or conclusion to find the "correct way" (tm) for oneself. Therefore sharing my thoughts here to help others and put them up for discussions to enhance them. Thanks.
So starting with picking-up the original question:
Then enriching it with the typical wishes and specifiying some presettings:
The point of view differs on what a "100%" backup is. Here are two typical ones.
git is a developer tool and supports this point of view via git clone --mirror
and git bundle --all
.
git gc
)git is a developer tool and leaves this to the admin. Backup of the git configuration and OS configuration should be seen as separated from the backup of the content.
Most of them are generic for backups.
git gc --auto
git bundle --all
git bundle verify
.git clone --mirror
git fsck
.A cold-copy backup can always do a full file backup: deny all accesses to the git repos, do backup and allow accesses again.
File backups cannot be done with active repos due to risk of corrupted data by on-going commits. A hot-copy provides a fixed state of an active repository for backup purposes. On-going commits do not affect that copy. As listed above git's clone and bundle functionalities support this, but for a "100% admin" backup several things have to be done via additional commands.
git bundle --all
to create full/incremental dump files of content and copy/backup configuration files separately.git clone --mirror
, handle and copy configuration separately, then do full file backup of mirror.
Upvotes: 21
Reputation: 29
If it is on Github, Navigate to bitbucket and use "import repository" method to import your github repo as a private repo.
If it is in bitbucket, Do the otherway around.
It's a full backup but stays in the cloud which is my ideal method.
Upvotes: -1
Reputation: 132071
Whats about just make a clone of it?
git clone --mirror other/repo.git
Every repository is a backup of its remote.
Upvotes: 79
Reputation: 1070
The correct answer IMO is git clone --mirror. This will fully backup your repo.
Git clone mirror will clone the entire repository, notes, heads, refs, etc. and is typically used to copy an entire repository to a new git server. This will pull down an all branches and everything, the entire repository.
git clone --mirror [email protected]/your-repo.git
Normally cloning a repo does not include all branches, only Master.
Copying the repo folder will only "copy" the branches that have been pulled in...so by default that is Master branch only or other branches you have checked-out previously.
The Git bundle command is also not what you want: "The bundle command will package up everything that would normally be pushed over the wire with a git push command into a binary file that you can email to someone or put on a flash drive, then unbundle into another repository." (From What's the difference between git clone --mirror and git clone --bare)
Upvotes: 9
Reputation: 768
Here are two options:
You can directly take a tar of the git repo directory as it has the whole bare contents of the repo on server. There is a slight possibility that somebody may be working on repo while taking backup.
The following command will give you the bare clone of repo (just like it is in server), then you can take a tar of the location where you have cloned without any issue.
git clone --bare {your backup local repo} {new location where you want to clone}
Upvotes: 0
Reputation: 25536
You can backup the git repo with git-copy at minimum storage size.
git copy /path/to/project /backup/project.repo.backup
Then you can restore your project with git clone
git clone /backup/project.repo.backup project
Upvotes: 3
Reputation: 8509
cd /path/to/backupdir/
git clone /path/to/repo
cd /path/to/repo
git remote add backup /path/to/backupdir
git push --set-upstream backup master
this creates a backup and makes the setup, so that you can do a git push to update your backup, what is probably what you want to do. Just make sure, that /path/to/backupdir and /path/to/repo are at least different hard drives, otherwise it doesn't make that much sense to do that.
Upvotes: 0
Reputation: 22726
Expanding on some other answers, this is what I do:
Setup the repo: git clone --mirror user@server:/url-to-repo.git
Then when you want to refresh the backup: git remote update
from the clone location.
This backs up all branches and tags, including new ones that get added later, although it's worth noting that branches that get deleted do not get deleted from the clone (which for a backup may be a good thing).
This is atomic so doesn't have the problems that a simple copy would.
See http://www.garron.me/en/bits/backup-git-bare-repo.html
Upvotes: 28
Reputation: 337
use git bundle, or clone
copying the git directory is not a good solution because it is not atomic. If you have a large repository that takes a long time to copy and someone pushes to your repository, it will affect your back up. Cloning or making a bundle will not have this problem.
Upvotes: 6
Reputation: 4562
As far as i know you can just make a copy of the directory your repo is in, that's it!
cp -r project project-backup
Upvotes: -8
Reputation: 4434
Everything is contained in the .git
directory. Just back that up along with your project as you would any file.
Upvotes: 4