Git 使用'删除所有提交;推送f原点主控器';有办法恢复吗?

Git 使用'删除所有提交;推送f原点主控器';有办法恢复吗?,git,github,push,commit,Git,Github,Push,Commit,我使用'git push-f origin master'提交,它删除了我以前的所有提交。有什么方法可以恢复它们或至少显示我以前的所有提交吗?您可以尝试以下方法: $ git reflog # show working tree, copy the last commit-hash that have all codes $ git checkout <commit-hash> # checkout to that commit, now yo

我使用'git push-f origin master'提交,它删除了我以前的所有提交。有什么方法可以恢复它们或至少显示我以前的所有提交吗?

您可以尝试以下方法:

$ git reflog                   # show working tree, copy the last commit-hash that have all codes
$ git checkout <commit-hash>   # checkout to that commit, now you've all codes
$ git checkout -b new-master   # create a new branch named new-master & checkout.
$ git branch -D master         # delete your local master branch
$ git checkout -b master       # create master from current commit and checkout to master
$ git push -f origin master    # push all the codes to remote  
$git reflog#显示工作树,复制包含所有代码的最后提交哈希
$git checkout#签出该提交,现在您拥有所有代码
$git checkout-b new master#创建一个名为new master&checkout的新分支。
$git branch-D master#删除您的本地master分支
$git checkout-b master#从当前提交并签出到master创建master
$git push-f origin master#将所有代码推送到远程

使用git reflog查看缺少的提交 然后,找到您想要的提交,并使用git reset来重置它们
重置已删除的提交后,然后使用git push origin master将更改推送到源站

您可以使用本地git reflog或远程存储库中的reflog,并找到要还原到的提交

git reflog //at local level
git reflog show remotes/origin/master // at remote level: 
reflog以相反的时间顺序显示对HEAD的所有更改的列表。第一列中的has是执行右侧操作后的HEAD值。因此,您可以找到提交散列,并通过签出其散列来签出要返回回购的提交

git checkout <commit-hash>
将其推回远程存储库

git push -f origin master
然而,这将修复您的远程主机,但请注意,您的本地主机仍然是您引入破坏性更改的主机,因此,如果不需要对其进行更改,您可以使用以下命令将其删除

git branch -D master , 
或者,您可能需要执行一些操作,如cherry pick,以选择丢失的所有提交


PS:您还可以找到git fsck更适合于恢复丢失的提交

可能重复使用的
git reflog
,并找到丢失的提交。如果您有一个从
master
派生的现有分支,其中包含缺少的提交,那么您也可以使用这些分支。的可能重复
git branch -D master ,