简体   繁体   English

我们可以从现有的本地存储库中重新放入一个git存储库

[英]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. 由于git是一个分布式VCS,因此它应该具有对存储库所做更改的完整历史记录。

So can we extract that 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服务器。

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). 因此,您需要明确指出要返回的修订版(Git可能不知道您最初克隆的修订版,但可能您可以弄明白)。 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. 第一步是从本地磁盘克隆到不同目录中的本地磁盘,这样您就可以保持现有工作不变。

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. 如果我理解正确,你从一个远程仓库克隆你的项目(调用本地仓库 - local_repo_1 ),之后你对它进行了一些更改(uncommited),现在你想拥有原始克隆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 离开你的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 并将其重置为所需的SHA
    git reset --hard SHA

  3. And finally you can go back to your local_repo_1 and apply your stash 最后,您可以返回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_1 :您在裸仓库上方所做的更改,恢复到原始格式。
local_repo_2 : a copy of the remote repo without your local changes local_repo_2 :没有本地更改的远程仓库的副本

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. 您将在早期克隆您的仓库。

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
}

你可以做一个新的分支

git checkout -b original-state REV

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 git如何清理克隆的存储库并重新克隆 - git how to clean a cloned repository and reclone 从GitHub上的仓库Git签出到基于SVN的现有本地仓库 - Git checkout from repository on GitHub into an existing local repository based on SVN 从现有本地存储库在Git中创建中央存储库(在Windows中) - Creating central repository in Git from existing local repository (in Windows) 将本地git存储库与现有存储库链接 - Link a local git repository with an existing repository 是否可以为本地存储库创建Git参考存储库? - Can a Git reference repository be created for a local repository? 如何从当前现有的本地git存储库中最佳设置“中央” git存储库? - How to best set up a “central” git repository from a current existing local git repository? 从现有的本地分支创建一个新的git存储库 - Create a new git repository from a existing local branch 我们可以从 GIT 存储库导出本地提交并将它们导入另一个系统吗 - Can we export local commits from GIT repository and import them on another system 从本地存储库中删除文件/文件夹后,如何从远程git存储库中获取文件/文件夹? - How to get files/folders from a remote git repository after we delete them from our local repository? Git:将现有的本地存储库连接到现有的远程存储库 - Git: Connect existing local repository to existing remote repository
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM