Benjohn
Benjohn

Reputation: 13887

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?

Upvotes: 2

Views: 156

Answers (2)

Vampire
Vampire

Reputation: 38734

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


If you really want to clone and save the time and bandwith for download, 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.

Upvotes: 5

thorolin
thorolin

Reputation: 25

You can git clone /local/git/repo from the local repo and use

git remote rename origin old_origin

git remote add origin <PATH>

to add the remote repo. and maybe delete the initial repo with

git remote remove old_origin

Use git remote -h for more information

Upvotes: 0

Related Questions