我们可以从现有的本地存储库中重新克隆一个 git 存储库吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22293118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:51:32  来源:igfitidea点击:

Can we reclone a git repository from the existing local repository

gitgit-clonegit-revert

提问by Priyank

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

由于 git 是分布式 VCS,因此它应该具有对存储库所做更改的完整历史记录。

So can we extractthat version of repo which was cloned first?

那么我们可以提取首先克隆的那个版本的 repo 吗?

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).

实际上,我对现有的本地存储库进行了很多更改。并且不想因丢失数据而恢复到原始状态。但是我想使用现有的 git 对象(blob/tree)将原始存储库(我最初克隆的)提取到其他位置。

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

注意:我现在无法访问 git 服务器。

采纳答案by John Zwinck

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.

因此,您需要明确确定要返回到哪个修订版(Git 可能不知道您最初克隆的是哪个修订版,但您可能可以弄清楚)。第一步只是从本地磁盘克隆到不同目录中的本地磁盘,这样您就可以保持现有工作不变。

回答by brokenfoot

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_1and not from remote.

如果我理解正确,您从远程存储库(调用本地存储库 - local_repo_1)克隆了您的项目,之后您对其进行了一些更改(未提交),现在您想要获得原始克隆的 git 存储库的另一个副本,但是从的local_repo_1,而不是从远程。

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_1and apply your stash
    git stash apply

  1. 将您的工作保存在藏匿处
    git stash save stash_name //give any name to your stash, say local_repo_2

  2. 现在你只剩下从远程克隆的裸仓库,你可以通过以下方式克隆它:
    从你的 git repo
    cd ..
    clone移出
    git clone /path/to/local_repo_1 local_repo_2 // local_repo_2 is the new repo
    如果你也有一些本地提交,然后执行 a
    git log
    并将其重置为所需的 SHA
    git reset --hard SHA

  3. 最后你可以回到你的local_repo_1并应用你的藏匿处
    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

瞧,现在您拥有:
local_repo_1:您在裸仓库之上所做的更改,恢复到其原始形式。
local_repo_2:没有本地更改的远程仓库的副本

回答by PRASANNA SARAF

You can add this script to your bash

您可以将此脚本添加到您的 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
}

回答by VinayVeluri

Yes you can do it absolutely.

是的,你绝对可以做到。

Try something like this

尝试这样的事情

Lets say you have cloned to

假设你已经克隆到

c:/originalRepository

switch to a new folder and say

切换到一个新文件夹并说

git clone file:///c:/originalRepository

You will have your repo as cloned earlier.

您将拥有之前克隆的仓库。

回答by Michael Ver

You could just make a new branch

你可以创建一个新的分支

git checkout -b original-state REV