强制推送后恢复文件历史

时间:2015-12-12 23:26:15

标签: git github git-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

现在,由于存储库只显示一个(强制)提交,因此没有任何文件保留其编辑历史记录。

有没有办法让这个存储库返回编辑历史记录?

我读了一些关于/撤消强制推送后恢复的问题,但是1)我不知道他们在谈论什么,2)我想我可能因为我还有旧的而得救。我迁移之前使用的旧文件夹中的git文件夹。

1 个答案:

答案 0 :(得分:1)

不幸的是,据报道,您无法在遥控器上使用git refloggit can I view the reflog of a remote?

如果您有旧的.git文件夹,我认为您应该保存。尝试在包含git reset --hard文件夹副本的文件夹中运行.git

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

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

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

步骤1:创建一个新目录:c:\ temp \ project。

第2步:打开一个shell。

步骤3:将目录更改为c:\ temp \ project。

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

第5步:输入git reset --hard

步骤6:再次看到你在c:\ temp \ project中有文件。

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