How to git pull remote and (re)initialize the local project after .git removal?

﹥>﹥吖頭↗ 提交于 2019-12-12 02:46:00

问题


Hard to explain, but basically I have a cloned remote repository that has had its local .git information removed. Some work has been done; but now I need to integrate it back into the remote repo; how do I 'init' the local project with the remote git repo? I know I can git init but I do not want to lose history, or commits, etc.


回答1:


Clone the project once more, then use cp or rsync command to overwrite all the project files in worktree by what you have modified.

To prevent potential conflict between your local change and remote, after clone the remote, you should create a new local branch and overwrite its worktree with your local modification. then, you merge this local branch with the one tracking the remote.




回答2:


Assuming that the SHA of the last commit you had checked out was abc123, do the following:

git init .
git checkout -b my_branch (where my_branch is a new branch name you made up)
git remote add origin <url of your remote repository>
git fetch
git reset abc123
git commit -am "Here are my changes"
git checkout master (or whatever branch you were on before you deleted .git)
git merge my_branch

If you don't know the exact commit you were last on, this will work if you pick any commit that is before the actual commit you were last on; the further back you go, the more merge conflicts you will have. Don't pick a commit that happened after the the actual commit you were last on, as that will cause you to lose changes.



来源:https://stackoverflow.com/questions/35354184/how-to-git-pull-remote-and-reinitialize-the-local-project-after-git-removal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!