Benjohn Benjohn - 8 months ago 35
Git Question

Clone a local git repo as if I had cloned from its remote

I have a local git repo that I cloned from a remote (github, say). The branches in this have associated upstreams that they pull and push from.

I'd like to make a new local copy of this repo so that I can be simultaneously developing on different branches locally.

I could just

git clone …
from the remote repo, but this will take a while over a slow link.

Instead, I'd like to
git clone
from the local repo, but have the effect be as if I had done a
git clone
of the remote. So, branches in the new copy will pull and push from the remote. For any other operation that needs a remote it will use the original remote.

How can I do this? … should I just
cp -r repo repo-clone

Answer Source

For this you do the clone form the remote as usual, but you add --reference <local repository> to use the Git objects from the local repository instead of downloading them from the remote. This will use the objects from the reference repository and not copy them, so it could happen that some Git cleanup deletes objects that you need in your second clone if you e. g. delete a branch in the reference repository that is still in the new repository and the commits are not reachable anymore in the reference repository. If you want to spend additional diskspace to overcome this, use additionally --dissociate to actually copy the objects from the reference repository.

If you actually don't want a new clone, but just checkout multiple worktrees for different branches, you might also consider using `git worktree' instead.