简体   繁体   English

Git:将本地存储库复制到另一个文件夹

[英]Git: copying a local repo to another folder

Say that I have a local git repo ("A") with a bunch of commits that I don't want to publish. 假设我有一个本地git repo(“ A”),其中包含一堆我不想发布的提交。

If I copy everything that is in this local repo "A" except for the git directory into a new folder (let's call it "B") and git init another repo there, is there any risk of this somehow messing up the first named local repo ("A") and losing the commit history for that repo? 如果我将本地目录“ A”中的所有内容(除了git目录之外)复制到新文件夹中(我们将其称为“ B”),然后在其中git init另一个目录中进行复制,是否有可能以某种方式弄乱第一个命名的本地回购(“ A”)并丢失该回购的提交历史记录?

The .git sub-directory turns a directory into a git repository. .git子目录将目录转换为git存储库。 If you remove it (or copy everything but the .git subdirectory) then you have a project that is not a git repository and it's not linked to any git repository in any way. 如果将其删除(或复制除.git子目录以外的所有内容),则您的项目不是git存储库,并且不会以任何方式链接到任何git存储库。

Running git init in the "B" directory creates a new git repository that is not linked in any way to the original repository ("A") or any other repository. 在“ B”目录中运行git init会创建一个新的git存储库,该存储库未以任何方式链接到原始存储库(“ A”)或任何其他存储库。

But keep in mind that after git init , the "B" repository is a different project (with a fresh, empty history) than the old "A" repository and linking them (even possible) will generate duplicate commits and conflicts. 但是请记住,在git init ,“ B”存储库与旧的“ A”存储库是一个不同的项目(具有新的,空的历史记录),将它们链接(甚至可能)将产生重复的提交和冲突。 (Thanks @NickVolynkin for pointing that out). (感谢@NickVolynkin指出这一点)。

The usual solution when you have some commits you don't want to publish is to put them on a new branch and remember to not push it. 当您不想提交某些提交时,通常的解决方案是将它们放在新的分支上,并记住不要push它。

Copy with Archive Settings 使用存档设置复制

From a parent directory: 从父目录:

cp -a repo-A repo-B

will make a duplicate repository that won't modify anything in A with the possible exception of atime (depending on your filesystem), which Git doesn't track anyway. 将创建一个重复的存储库,该存储库不会修改A中的任何内容,但可能会更改atime(取决于您的文件系统),但Git始终不会跟踪。

Both repositories will be functionally identical, including history and uncommitted work. 两个存储库在功能上都是相同的,包括历史记录和未提交的工作。 You can then make whatever changes you like independently to either repository. 然后,您可以独立地对任一存储库进行任何所需的更改。

Caveats 注意事项

If you've cloned with the --shared flag, or set .git/objects/info/alternates you may have shared files between repositories even if you've created an archive copy. 如果使用--shared标志进行克隆,或者设置了.git/objects/info/alternates ,则即使创建了归档副本,也可能在存储库之间共享文件。 This is not the general case, but is an exception you must be aware of and take special precautions with if you've done this. 这不是一般情况,但是这是一个例外,您必须意识到这一点,并采取特殊的预防措施。 Caveat emptor. 买者自负。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM