简体   繁体   English

将目录初始化为具有现有相同远程目录的git存储库

[英]Initialize a directory as a git repository with an existing identical remote

Suppose I have a remote repository (such as on GitLab) already set up. 假设我已经建立了一个远程存储库(例如在GitLab上)。

And I have a directory on my computer that is identical to that remote repository, but is not set up as a git repository. 我的计算机上有一个目录与该远程存储库相同,但未设置为git存储库。

How could I initialize my directory as a git repository, matching the existing remote, with its commit history etc? 如何将目录初始化为git存储库,将现有远程及其提交历史记录与之匹配?

In other words, how can I achieve the equivalent of cloning that repository, but using what I already have, instead of downloading everything again? 换句话说,我如何才能实现克隆该存储库的等同功能,而又使用现有资源而不是再次下载所有内容?

And if there are any differences with my directory, I would like them to be detected as changed and ready to commit. 并且,如果我的目录存在任何差异,我希望将它们检测为已更改并准备提交。

The reason for this is that we are switching version control systems, so currently everyone in our team has the same project synchorized with Unity Collab. 原因是我们要切换版本控制系统,所以目前我们团队中的每个人都有与Unity Collab同步的同一项目。 I initialized my copy and published it to GitLab, and I would like others to be able to enable the usage of git in their existing copy, without having to clone it again. 我初始化了副本并将其发布到GitLab,我希望其他人能够在现有副本中启用git,而不必再次克隆它。 Especially if they have changes in progess. 特别是如果他们的进度有所变化。 Plus it lets them continue to use collab, as I've tested cloning and it breaks collab. 另外,正如我已经测试过克隆一样,它可以让他们继续使用合作,并且破坏了合作。

I found that copying my .git directory to the other projects worked as desired, however the .git directory is very large and becomes out of date after a commit. 我发现将.git目录复制到其他项目可以按预期工作,但是.git目录很大,并且在提交后已过时。 So I'm wondering if there is a better way to do this. 所以我想知道是否有更好的方法可以做到这一点。

as simple as this: 如此简单:

git init .
git remote add origin [URI TO THE EXISTING REMOTE]
git fetch           # transfer remote history to your PC
git checkout master # checkout files of master branch

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

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