0

The situation is this; we have a dev server with our project on there. I have the same project locally. There is no repository for this project yet. The files are equal, or so we hope. I git init the server files using SSH, git remote add, git add . the files, git commit -m and git push -u origin master. Again, on the sever.

Now, I don't want to git clone this entire repository because I already have the files locally. Is there a clean way I can git init somehow within my local files so that I don't have to git clone the entire project AND is there a way to nicely handle any possible differences within the local files compared to the now pushed server files.

The attempts thusfar make git not understand that the local files are the same as the server files (in the now online repo) and says the entire files are overwritten.

Any help is appreciated.

Ken
  • 2,859
  • 4
  • 24
  • 26
  • Possible duplicate of [syncing a local directory with an existing git repository](https://stackoverflow.com/questions/20415028/syncing-a-local-directory-with-an-existing-git-repository) – bitoiu Mar 13 '19 at 10:29
  • @bitoiu Not a duplicate – Ken Mar 13 '19 at 10:30
  • 1
    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) – Thomas Kainrad Mar 13 '19 at 10:54

1 Answers1

0

This answer discusses a very similar use case.

The basic idea is to only copy the .git folder into your existing project root folder.

The following example, taken directly from the linked answer, works with git reset --hard HEAD. If you want to try and use also the local changes, try to use git reset HEAD before.

The existing-dir is your local directory that matches the files in the repo-to-clone repository.

# Clone just the repository's .git folder (excluding files as they are already in
# `existing-dir`) into an empty temporary directory
git clone --no-checkout repo-to-clone existing-dir/existing-dir.tmp # might > want --no-hardlinks for cloning local repo


# Move the .git folder to the directory with the files.
# This makes `existing-dir` a git repo.
mv existing-dir/existing-dir.tmp/.git existing-dir/

# Delete the temporary directory
rmdir existing-dir/existing-dir.tmp
cd existing-dir

# git thinks all files are deleted, this reverts the state of the repo to HEAD.
# WARNING: any local changes to the files will be lost.
git reset --hard HEAD
Thomas Kainrad
  • 2,542
  • 21
  • 26