为了账号安全,请及时绑定邮箱和手机立即绑定

如何从错误的git push -f origin master恢复?

如何从错误的git push -f origin master恢复?

Git
侃侃无极 2019-12-25 11:08:10
我只是使用--forceoption 将错误的源提交给我的项目。是否可以还原?我知道以前的所有分支都已使用-foption 覆盖,因此我可能搞砸了以前的修订版。
查看完整描述

3 回答

?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

Git通常不会丢弃任何东西,但是从中恢复可能仍然很棘手。

如果您有正确的来源,则可以使用该--force选项将其推入遥控器。除非您告知,否则Git不会删除任何分支。如果您实际上丢失了提交,请查看此有关恢复提交的有用指南。如果您知道想要的提交的SHA-1,那么可能就可以了。

最好的事情:备份所有内容,然后查看本地存储库中仍然存在的内容。如果可能,在遥控器上执行相同的操作。使用git fsck,看看你是否能恢复的东西,最重要的不运行git gc

最重要的是,--force除非您真的很认真,否则不要使用该选项。


查看完整回答
反对 回复 2019-12-25
?
SMILET

TA贡献1796条经验 获得超4个赞

解决方案已经在这里提到


# work on local master

git checkout master


# reset to the previous state of origin/master, as recorded by reflog

git reset --hard origin/master@{1}


# at this point verify that this is indeed the desired commit.

# (if necessary, use git reflog to find the right one, and

# git reset --hard to that one)


# finally, push the master branch (and only the master branch) to the server

git push -f origin master


查看完整回答
反对 回复 2019-12-25
  • 3 回答
  • 0 关注
  • 516 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信