Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I copy/clone a git repository without wasting space?

Tags:

git

I am doing some work to track down a perceived bug in a git support tool, and I want to copy a repository (possibly many times) for experimental purposes. I would like for hard links to be created to the original repository object database so as not to waste disk space storing multiple copies of identical objects with identical SHA values. git clone accomplishes this nicely, but unfortunately I can't seem to find a way to make git clone give me a new copy of the repository set up like the original repository:

  1. the clone points back to the copied repository as its origin, but I want it to have the same origin as the copied repository
  2. the clone winds up with only one branch, but I want it to have all of the branches of the copied repository

I can ameliorate some of this by using git clone --mirror, but that will only work to create a bare repository, and the repository I am experimenting on is not bare, and the support tool I am trying to debug does not function on bare repositories.

I thought about writing a quick shell loop to make a new object database full of hardlinks to the original object database, and then copying everything else out of the original .git directory, and then doing a git checkout, but I am concerned that this is complicated enough to be error prone and that I don't know enough to perform this kind of low-level surgery correctly and will build a repository with subtle errors. Should this approach work, in theory?

Better yet, is there a way to pass options to git clone to make it do what I want?

Is there a way I can create a new repository but tell it that its object database is in another directory, basically telling it to share the object database from the original repository?

If none of the above works, then I'm left with only a cp -pr, which is going to consume a lot of space and be time consuming.

like image 542
skiphoppy Avatar asked Jun 30 '09 15:06

skiphoppy


People also ask

Can I just copy a git repository?

You can clone a repository from GitHub.com to your local computer to make it easier to fix merge conflicts, add or remove files, and push larger commits. When you clone a repository, you copy the repository from GitHub.com to your local machine.

How do I duplicate a git repository?

Open TerminalTerminalGit Bash. Create a bare mirrored clone of the repository. Set the push location to your mirror. As with a bare clone, a mirrored clone includes all remote branches and tags, but all local references will be overwritten each time you fetch, so it will always be the same as the original repository.

How do I clone an entire git repository including branches?

To clone a branch in a git repository, start by cloning the master repository using the git clone command. Once complete, navigate into the repo directory. The command will show the branches that are available in the local repository. To view even the remote branches, use the -a flag.


1 Answers

I think you're looking for

git clone --reference=/path/to/existing/repo url

In git lingo this known as using "alternates". There are potential problems with this option, as mentioned in the git clone help but it may suit your purposes.

You may also be interested in Dustin's helper git-alternates.

like image 105
Pat Notz Avatar answered Sep 24 '22 00:09

Pat Notz