Git 远端回滚

查找提交记录

$ git log

commit 16577b2c2b8d1f6e92528aa417acc83dcd2f4b64
Author: 陈子云 <>
Date:   Wed Mar 8 17:31:26 2017 +0800

    commit最近记录1

commit fec5d9f293c4aecad66c9a1414d2e3045be04aad
Author: 陈子云 <>
Date:   Mon Feb 6 15:58:29 2017 +0800

    commit最近记录2

commit 0279a5684d59afbc0ed568e1c2319395da0a3eeb
Merge: 1e93cdd 64f9f9e
Author: 陈子云 <>
Date:   Wed Jan 11 14:06:20 2017 +0800

    commit最近记录3

找到最近若干次的commit记录,假如我们需要舍弃记录1和记录2,希望直接把远端的分枝回滚到记录3。

强制回滚

$ git reset --hard 0279a568

HEAD is now at 0279a56 commit最近记录3

# 如果只是要回到之前一个提交记录
# git reset --hard HEAD^1

其中--hard后面的八位字符时上面提交记录里的commit后面的一串的前八位。

$ git status

On branch master
Your branch is behind ‘origin/master‘ by 2 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

查看得知本地回滚成功,但是远程的库还没回滚。

远程回滚

$ git push -f origin master

Total 0 (delta 0), reused 0 (delta 0)
To git.souche.com:fis-projects/dafengche-maintain-query-f2e.git
+ 16577b2...0279a56 master -> master (forced update)

成功的提示如上。但是如果执行后不成功,提示如下的话,则需要上 gitlab/github 上设置 Protected Branches 了,为的是去除本分支的保护后才能强制覆盖提交。

$ git push -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 git.souche.com:fis-projects/dafengche-maintain-query-f2e.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to ‘:fis-projects/dafengche-maintain-query-f2e.git‘

Git 远端回滚

相关推荐