0

Suppose I have a remote repository (such as on GitLab) already set up.

And I have a directory on my computer that is identical to that remote repository, but is not set up as a git repository.

How could I initialize my directory as a git repository, matching the existing remote, with its commit history etc?

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. 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. 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. So I'm wondering if there is a better way to do this.

Lamp
  • 589
  • 4
  • 10
  • 2
    Possible duplicate of [How do I clone into a non-empty directory?](https://stackoverflow.com/questions/2411031/how-do-i-clone-into-a-non-empty-directory) – phd Jun 25 '18 at 20:46

1 Answers1

4

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
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
  • is that meant to be `remote add` not `add remote`? – joel Jun 25 '18 at 20:24
  • @joelb: yes, ofcause!, thanks for pointing out, I'm correcting it. – Timothy Truckle Jun 25 '18 at 20:25
  • This isn't working for me. Firstly `git fetch` doesn't appear to do anything; the commit history is left empty. Then `git checkout master` won't work because there are uncommitted changes. And if I make an initial commit, my commit history still doesn't match. – Lamp Jul 18 '18 at 06:16
  • I found that using `git reset origin/master` instead of `git checkout master` worked for me. – Lamp Jul 18 '18 at 06:45