Git删除暂存区和本地项目下所有文件目录并推送到远程仓库,回滚到某个commit id,再次推送到远程仓库失败的解决方法
Git删除暂存区和本地项目下所有文件目录并推送到远程仓库,回滚到某个commit id,再次推送到远程仓库失败的解决方法
**
这是搭建Gitlab时误操作遇到的坑,请谨慎操作,数据无价,代价很大
**
测试:
[[email protected] mytest]# ls
review texun1 texun2
[[email protected] mytest]# rm -rf review texun1 texun2
[[email protected] mytest]# git rm -r --cached .
[[email protected] mytest]# git commit -m “rm all”
deleted: “review/\347\275\221\347\273\234\345\215\207\347\272\247\351\241\271\347\233\256.pdf”
deleted: “review/\351\235\242\350\257\225\351\242\230\346\225\264\347\220\206.pdf”
deleted: texun1/maven.md
deleted: texun2/maven.md
[[email protected] mytest]# git push -u origin master
[[email protected] mytest]# ls #为空
直接恢复就是回到上次操作
[[email protected] mytest]# git reset --hard HEAD^
[[email protected] mytest]# ls
review texun1 texun2
或者恢复到某个commit id
[[email protected] mytest]# git log
[[email protected] mytest]# git reset --hard a612edb2d5d33524d46a274808e1a511b70830a2
[[email protected] mytest]# ls
review texun1 texun2
强推到远程也会失败,提示本地仓库落后与远程仓库(新旧)
[[email protected] mytest]# git push -u -f origin master
Total 0 (delta 0), reused 0 (delta 0)
remote: GitLab: You are not allowed to force push code to a protected branch on this project.
To [email protected]:devops/myweb.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to ‘[email protected]:devops/myweb.git’
解决方法:
方法一:
登录到gitlab,在项目的左侧栏设置 ——》版本库
分支保护 ——》点击展开
点击——》解除保护
然后回到终端操作推送到远程仓库
[[email protected] mytest]# git push -u -f origin master #要强制推送不然会有冲突
在推送成功后把gitlab设置还原
方法二:
删除.git目录
[[email protected] mytest]# rm -rf .git
再次初始化仓库
[[email protected] mytest]# git init
跟踪恢复后的目录文件,比提交打暂存区
[[email protected] mytest]# git add .
[[email protected] mytest]# git commit -m “rm after,reset 1.0”
链接远程仓库,并同步远程仓库到本地仓库
[[email protected] mytest]# git remote add origin [email protected]:devops/myweb.git
[[email protected] mytest]# git pull origin master
就能再次成功推到远程
[[email protected] mytest]# git push -u origin master