5.1.1. GIT远程仓库回退

问题:把错误的代码提交到了远程git分支。怎么将远程分支回退

用以下步骤回退:

5.1.1.1. 1. 查看分之操作记录找到需要回退的commitId

比如下面这个我们回退到 7cb66c8 这个正常的提交

$ git reflog
cf3c612 HEAD@{0}: commit: 代码正常的
626e927 HEAD@{1}: revert: Revert "更新日志"
e6acf2d HEAD@{2}: checkout: moving from master to e6acf2d
7cb66c8 HEAD@{3}: checkout: moving from e6acf2d2bfedea95ccf9b55ba5b2378486be24cb to master
e6acf2d HEAD@{4}: commit: 更新日志
7cb66c8 HEAD@{5}: checkout: moving from master to 7cb66c893b61f084d2cb3037c9e8cd07aebd391a
7cb66c8 HEAD@{6}: reset: moving to 7cb66c893b61f084d2cb3037c9e8cd07aebd391a
85de426 HEAD@{7}: pull --progress --no-stat -v --progress origin master: Fast-forward
7cb66c8 HEAD@{8}: commit: 更新日志
f3240d5 HEAD@{9}: commit: 增加input自动获取焦点
83310f3 HEAD@{10}: commit: 方块云皮肤优化
ec5ac41 HEAD@{11}: commit: 组件位置优化。弹窗登录移动到home目录
972c848 HEAD@{12}: commit: 增加登录皮肤:方块云
900b93b HEAD@{13}: commit: fix bug: 注销回调操作 功能异常。 this域变了
84d4a96 HEAD@{14}: commit: 增加注销的时候访问后台功能
b4970dd HEAD@{15}: commit: doc
6c2c092 HEAD@{16}: commit (initial): Init

5.1.1.2. 使用新的提交覆盖历史提交

使用以下命令提交新的commitId覆盖掉回退到的提交点中间的提交历史。在执行该命令后,会打开一个vim编辑框,让你填写commit信息。保存后或则取消都会提交。

$ git revert 7cb66c8
[detached HEAD 87f88cd] Revert "更新日志"
 1 file changed, 9 deletions(-)

Warning: Your console font probably doesn't support Unicode. If you experience strange characters in the output, consider switching
 such as Lucida Console!

我们查看当前的状态,会发现 Head detached(版本游离与远程仓库脱节) 了。 回退的版本id落后远程仓库的id

$ git status
HEAD detached from e6acf2d
nothing to commit, working directory clean

使用git push命令将会失败;提示如下:

$ git push
包括 使用
$ git push -f
fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
state now, use

    git push origin HEAD:<name-of-remote-branch>

上面的命令告诉我们,要把当前的提交点提交到远程仓库的哪个版本?

$ git push -f origin HEAD:master

由于这里只有一个master分支,直接提交到master分支上面。却失败了

$ git push -f origin HEAD:master
Counting objects: 22, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (13/13), 1.36 KiB | 0 bytes/s, done.
Total 13 (delta 7), reused 0 (delta 0)
remote: GitLab: You are not allowed to force push code to a protected branch on this project.
To git@tbgitbit:peter/tlz-vue-template.git
 ! [remote rejected] HEAD -> master (pre-receive hook declined)
error: failed to push some refs to 'git@xxx.git'

上面翻译一下,最重要的就是远程仓库拒绝了强制推送提交。

5.1.1.3. 关闭远程仓库的 保护权限

远程仓库的受保护分支权限先关闭:Protected branches(在web页面中操作的)

关闭之后再次执行命令

$ git push -f origin HEAD:master
Counting objects: 22, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (13/13), 1.36 KiB | 0 bytes/s, done.
Total 13 (delta 7), reused 0 (delta 0)
To git@xxx.git
 + 85de426...87f88cd HEAD -> master (forced update)

发现提交成功了;但是本地分支状态还是游离状态

$ git status
HEAD detached from e6acf2d
nothing to commit, working directory clean
(其实是上面这句话,让我误认为联想到切换到master分支上,直接使用pull将会自动删除该游离分支,但是最后果真切换回来了。误打误撞)

查看下现在我们所在的分支

$ git branch
* (detached from e6acf2d)
  master

发现还是在游离的分支上面。这个时候只要切换到master上面

$ git checkout master
Previous HEAD position was 87f88cd... Revert "更新日志"
Switched to branch 'master'
Your branch is behind 'origin/master' by 4 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

Warning: Your console font probably doesn't support Unicode. If you experience strange characters in the output, consider switching
 such as Lucida Console!

这里说 我本地的分支落后于远程仓库的4个提交。直接使用get pull 更新到最新的分支。这里就完成了远程分支的回退

但是:有一种情况就是,如果是多个人共同开发的话,其他已经修改的代码是否能被更新到回退的分支上面去? 这个还不知道

© All Rights Reserved            updated 2017-07-02 22:00:18

results matching ""

    No results matching ""