记录一次git回滚代码

老大临时让更新一版代码到本地,熟练的git fetch/git merge 之后,出来了一批改动的文件,但是并不是我改动的。

我以为是版本迭代出来的其他同事改的,我就直接给add commit到我的版本库里去了直接push完了之后上git上一看

显示我变更了10多个文件,直接给我吓尿了。还好近期开发任务不是很重,本着大不了删除重新fork的态度进行了回滚

下面进行正文:

1、查看自己库的版本

  命令 git reflog 出来结果如下

  或者在git上操作

 2、回滚指定版本

  git reset - -hard  你的commit id

3、然后推送到远程自己的库上去

  git push -f -u origin master   (github)

  git push origin master --force (gilab)

  这时可能会报错:

  Updates were rejected because the tip of your current branch is behind
  参考链接:https://segmentfault.com/q/1010000009800764/a-1020000009800939

  You are not allowed to force push code to a protected branch on this project

  开发团队对master分支仓库保护,你可以在网页中setting-repository解除锁定,如下图:(右下角unprotect)

 4、然后git push就能成功push到版本库中,再去网页上看版本也是要回退的版本了

原文地址:https://www.cnblogs.com/PrayzzZ/p/11642420.html