首页 > 解决方案 > 本地目录和远程存储库之间的连接丢失

问题描述

我有一个本地目录,是大约一年前通过克隆远程存储库创建的。从那时起,我对本地代码进行了数百次更改,但目录中不再存在 git 文件,这意味着与远程存储库的连接已丢失。同时,自从我第一次克隆远程存储库以来,它已经进行了数百次更改。

现在,我想重新建立该连接,并将远程存储库中的任何新更新拉到我的本地目录中。

我该怎么做呢?

标签: gitgithubatlassian-sourcetreegithub-desktop

解决方案


According to your comments you have lost your .git repository and your directory has a lot of updates from the last time you synced with the remote.

If you lost your .git repository then you cannot restore any commit which happened between the last push from you lost repository till it got lost.

What you can do, is to create another local repository from the remote one and manually merge your files into it.

  1. git clone your-remote-repo your-local-repo
  2. git checkout -b your-restore-branch, just make a different branch for restoring
  3. copy your files itno your-local-repo
  4. git diff --name-status will show the differences between the tip of the branch and the files which you copied
  5. manually review all the differences and decide what you want to do with them
  6. when you done your merge work, commit all the changes in the your-restore-branch by git commit; You might want to do several commits. do testing.
  7. check out the original branch git checkout master (or whatever branch name you had)
  8. git merge your-restore-branch --squash merge into the main branch, ignoring your manual merge history. then do your testing again and git commit for the results of the merge.
  9. when you done, you can do git push into the remote repo.

推荐阅读