Fork me on GitHub

Git远程库版本回滚

在git的一般使用中,如果发现错误的将不想staging的文件add进入index之后,想回退取消,这就叫做git代码库回滚: 指的是将代码库某分支退回到以前的某个commit id。可以使用命令:git reset HEAD <file>...,同时git add完毕之后,git也会做相应的提示,Git reset 是Git最常用的命令之一,也是最危险最容易误用的命令。 用法参考 Git学习笔记03--git reset

【本地代码库回滚】:

git reset --hard commit-id :回滚到commit-id,讲commit-id之后提交的commit都去除

git reset --hard HEAD~3:将最近3次的提交回滚

【远程代码库回滚】:

这个是重点要说的内容,过程比本地回滚要复杂

应用场景:自动部署系统发布后发现问题,需要回滚到某一个commit,再重新发布

原理:先将本地分支退回到某个commit,删除远程分支,再重新push本地分支

操作步骤:

1、git checkout the_branch

2、git pull

D:\FitProject\NPS>git pull
remote: Counting objects: 307, done
remote: Finding sources: 100% (245/245)
remote: Getting sizes: 100% (134/134)
remote: Compressing objects: 100% (31123/31123)
Rremote: Total 245 (delta 156), reused 242 (delta 154)
Receiving objects: 100% (245/245), 22.67 KiB | 0 bytes/s, done.
Resolving deltas: 100% (156/156), completed with 40 local objects.
From xxxxxxxxxxxx
    fe0d037..d159d4d  stable_chh_1127 -> origin/stable_chh_1127
Already up-to-date.

3、git branch the_branch_backup //备份一下这个分支当前的情况

D:\FitProject\NPS>git branch Geffdev_0926_backup

4、git reset --hard the_commit_id //把the_branch本地回滚到the_commit_id

D:\FitProject\NPS>git reset --hard 56f7c0d56befd4cad99a6017062824e073b56c01
HEAD is now at 56f7c0d 封装付款体现Relay接口

5、git push origin :the_branch //删除远程 the_branch

6、git push origin the_branch //用回滚后的本地分支重新建立远程分支

D:\FitProject\NPS>git push origin : Geffdev_0926
To xxxxxx
  ! [rejected]        Geffdev_0926 -> Geffdev_0926 (non-fast-forward)
  ! [rejected]        develop -> develop (non-fast-forward)
  ! [rejected]        master -> master (non-fast-forward)
  ! [rejected]        stable_chh_1127 -> stable_chh_1127 (non-fast-forward)
error: failed to push some refs to xxxxxx
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

D:\FitProject\NPS>git push origin  Geffdev_0926
Total 0 (delta 0), reused 0 (delta 0)
remote: Updating references: 100% (1/1)
To xxxxxx
  * [new branch]      Geffdev_0926 -> Geffdev_0926

7、git push origin :the_branch_backup //如果前面都成功了,删除这个备份分支

posted @ 2017-11-28 09:15 张善友 阅读(...) 评论(...) 编辑 收藏