简体   繁体   English

如何在不进行git克隆的情况下同步新的GIT存储库本地文件

[英]How to sync a new GIT repo local files without git cloning

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 . 我使用SSH git init服务器文件, git remote addgit add . the files, git commit -m and git push -u origin master . 文件git commit -mgit 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. 现在,我不想对整个存储库进行git clone,因为我已经在本地存储了文件。 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. 有没有一种干净的方法可以以某种方式在本地文件中进行git init ,这样就不必git clone整个项目,并且有一种方法可以很好地处理本地文件中与现在推送的服务器文件相比可能存在的差异。

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. 到目前为止的尝试使git无法理解本地文件与服务器文件相同(在现在的在线仓库中),并说整个文件都被覆盖了。

Any help is appreciated. 任何帮助表示赞赏。

This answer discusses a very similar use case. 这个答案讨论了一个非常相似的用例。

The basic idea is to only copy the .git folder into your existing project root folder. 基本思想是仅将.git文件夹复制到现有项目根文件夹中。

The following example, taken directly from the linked answer, works with git reset --hard HEAD . 以下示例直接取自链接的答案,适用于git reset --hard HEAD If you want to try and use also the local changes, try to use git reset HEAD before. 如果要尝试也使用本地更改,请尝试在之前使用git reset HEAD

The existing-dir is your local directory that matches the files in the repo-to-clone repository. existing-dir是您的本地目录,该目录与repo-to-clone存储库中的文件匹配。

 # 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 

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

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