Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we reclone a git repository from the existing local repository

Since git is a distributed VCS, it should have complete history of changes done to a repository.

So can we extract that version of repo which was cloned first?

Actually I have done a lot of changes in my existing local repo. and do not want to revert back to the original state by losing the data. But I want to extract the original repository(which I cloned initially) to some other location using existing git objects(blob/tree).

Note : I don't have the access to git server now.

like image 740
Priyank Avatar asked Mar 10 '14 06:03

Priyank


People also ask

Can I Reclone a repository?

About cloning a 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 mirror an entire existing git repository into a new one?

You first have to get the original Git repository on your machine. Then, go into the repository. Finally, use the --mirror flag to copy everything in your local Git repository into the new repo.

What happens if you clone an existing git repository?

Usage. git clone is primarily used to point to an existing repo and make a clone or copy of that repo at in a new directory, at another location. The original repository can be located on the local filesystem or on remote machine accessible supported protocols. The git clone command copies an existing Git repository.


3 Answers

Try this:

git clone SOURCE_PATH NEW_PATH # clones everything you have committed up to now
cd NEW_PATH                    # go to new clone, don't modify the pre-existing one
git reset --hard REV           # REV is the revision to "rewind" to (from git log)

So you need to figure out explicitly which revision to go back to (Git probably doesn't know which revision you originally cloned, but probably you can figure it out). The first step is just to clone from your local disk to your local disk in a different directory so you can keep your existing work untouched.

like image 92
John Zwinck Avatar answered Oct 04 '22 13:10

John Zwinck


If I understand correctly, you cloned your project from a remote repo (call the local repo - local_repo_1), after that you have made some changes to it (uncommited) and now you want to have another copy of the original cloned git repo, but from the local_repo_1 and not from remote.

You can do this in the following these steps:

  1. Save your work in a stash
    git stash save stash_name //give any name to your stash, say local_repo_2

  2. Now you'd be left with the bare repo that you cloned from the remote, you can clone it by:
    move out from your git repo
    cd ..
    clone
    git clone /path/to/local_repo_1 local_repo_2 // local_repo_2 is the new repo
    In case you had some local commits too, then do a
    git log
    and reset it to the desired SHA
    git reset --hard SHA

  3. And finally you can go back to your local_repo_1 and apply your stash
    git stash apply

Voila, now you have:
local_repo_1: your changes that you had made over and above the bare repo, back to its original form.
local_repo_2: a copy of the remote repo without your local changes

like image 38
brokenfoot Avatar answered Oct 04 '22 13:10

brokenfoot


You can add this script to your bash

reclone () {
    set -e
    basename=${PWD##*/}
    remoteurl=$(git remote get-url --push origin)
    cd ..
    echo $basename
    echo $remoteurl
    rm -rf $basename
    git clone $remoteurl
    cd $basename
    set +e
}
like image 38
PRASANNA SARAF Avatar answered Oct 04 '22 15:10

PRASANNA SARAF