强制推送后恢复文件历史记录

Recovering file history after a forced push

我刚刚尝试将本地计算机上某个存储库的文件迁移到另一个目录,但似乎失败得很惨。

将源文件复制粘贴到新文件夹后,我通过以下操作尝试在新目录中执行以下操作并提交到原始 GitHub 存储库:

git init
git add .
git commit -m 'migration'
git remote add origin https://github.com/UN/REP.git
git push origin master

这(我现在已经习惯了 git)给我一个错误:

 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/UN/REP.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

不知道为什么会这样,我只是天真地强行推送

git push -f origin master

现在,none 的文件保留了它们的编辑历史,因为存储库仅显示一个(强制)提交。

有什么方法可以取回此存储库的编辑历史记录吗?

我读了一些关于恢复的问题 after/undoing 强制推送但是 1) 我不知道他们在说什么 2) 我想我可能会因为我仍然有旧 .git 文件夹在我迁移前使用的旧文件夹中。

很遗憾,SO 上报告说您无法在遥控器上使用 git refloggit can I view the reflog of a remote?

如果你有旧的 .git 文件夹,我想你应该得救了。在包含 .git 文件夹副本的文件夹中尝试 运行 git reset --hard

我刚刚用我的一个回购协议尝试了这个。我只复制了 .git 文件夹并将其复制到一个空白文件夹中。然后我打开一个 shell 并导航到这个新文件夹,其中只包含 .git 文件夹,键入命令 git reset --hard 和 git 立即将文件放入最后一个分支我在文件夹里。

编辑: 根据下面的评论,我认为@MichaelChirico 误解了我的回答。让我试着说得更清楚。

假设您的 .git 目录位于 c:\Michael\project.

第一步:新建目录:c:\temp\project.

第 2 步:打开 shell。

第 3 步:将目录更改为 c:\temp\project.

第 4 步:键入 ls。您应该看到该目录包含 .git 而没有其他内容。

第 5 步:键入 git reset --hard

第 6 步:看到您现在又在 c:\temp\project 中有了文件。

第 7 步:如果文件不是您想要的分支,请使用 git checkout 更改分支。