作为备份的一种方法,我通过每隔一段时间提交新的更改,将文件夹结构推送到git存储库。在本例中,它是Linux用户$home文件夹的大部分。由于备份还包括各种应用程序(如邮件客户端、浏览器等)的本地文件夹,自然,即使在最近的git推送之后几秒钟,也会有一大堆新的和未提交的更改。
碰巧,我意外地破坏了我的~/.git/文件夹(用另一个存储库中的~/.git/文件夹覆盖它,所以它真的不见了)。我想通过从远程克隆git历史来恢复它,并保留所有文件与其存储库计数器部分的连接,但不希望本地文件被git clone或git pull覆盖。我试图使用.git/然后git fetch版本,通过跟踪this answer来恢复git reset文件夹。所有四个命令都完成了,没有错误。然而,就我的本地存储库而言,我陷入了一种困境:
git branch -av显示了正确的远程主机,其中包含正确的上次提交-但仅限于此;没有列出本地分支/主机。
git status显示:

On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    [...]

自上次提交和git push之后更改的本地文件未列出。
git log显示:fatal: your current branch 'master' does not have any commits yet
我尝试隐藏本地更改,以便git拉取并重新应用隐藏;但是,git stash save会导致:
fatal: bad revision 'HEAD'
fatal: bad revision 'HEAD'
fatal: Needed a single revision
You do not have the initial commit yet

git checkout master中止:
error: The following untracked working tree files would be overwritten by checkout:
    [...]
    [... looooong list of files and folders ...]
    [...]
Please move or remove them before you can switch branches.
Aborting

git rev-parse --abbrev-ref HEAD产生“HEAD”–如果一切正常,它应该输出一个分支名称或“master”(我使用此命令在bash的提示符中显示当前分支名称)。
有人能告诉我如何在不覆盖(未提交)更改的情况下“恢复”本地存储库和所有文件引用吗?answer from the other topic中似乎缺少什么?

最佳答案

一个技巧是将当前损坏的repo(缺少.git的repo)视为工作树(修改后的文件所在的树)
将repo克隆到其他位置,然后将文件从第一个工作树添加到第二个克隆的repo中:

cd /path/to/second/cloned/repo
git --work-tree=/path/to/first/repo add .
git commit -m "import work in progress"
git push

关于linux - 删除.git文件夹后,如何“只读”恢复本地GIT存储库和文件引用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50494751/

10-16 21:15