git版本回退

参考资料:
git 回退版本代码之后,强制将本地代码推送至远程仓库
git的版本回退教程

背景

由于代码合并或者被别的代码污染了现有的分支,我们想回退到历史上某个正确的版本

解决

版本回退

  1. 通过git log 查看提交的历史记录,版本号
Author: authorName <1001111u89432@qq.com>
Date:   Wed Nov 24 19:23:58 2021 +0800

    Merge branch 'master' into 'feature/update_price_rate'
    
    # Conflicts:
    #   grpc/proto/indent.pb.go
    #   grpc/proto/indent.pb.gw.go
    #   grpc/proto/indent.proto
    #   service/order.go

commit 6fbb4e7389c234cdc792a500483c30b5a598f265  # 要回退到这个版本
Author: authorName <1001111u89432@qq.com>
Date:   Mon Sep 6 15:38:39 2021 +0800

    删除多余调式代码

commit 299aab44a1e27644d567e50dae55c8951a3609b8
Author: authorName <1001111u89432@qq.com>
Date:   Mon Sep 6 12:02:49 2021 +0800

    修改sql
  1. 回退操作
git reset --hard 6fbb4e7389c234cdc792a500483c30b5a598f265
HEAD is now at 6fbb4e7 删除多余调式代码

检查本地是否回退成功
3. 回退成功后就是把本地的正确的提交到远程分支
但是到了这里,其实只是本地回退了版本而已,远程仓库的并没有回退。如果想要将本地的代码直接push到远程仓库则会报错如下:

$ git push
To gitee.com:kubernete/Performance-Test-Management-demo.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@gitee.com:kubernete/Performance-Test-Management-demo.git'
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.

这里会提示需要更新远程仓库,如果更新执行git pull的话,又会将刚才回退的版本下载回来。

此时需要强制将本地代码推送至远程仓库之中,使用git push -f origin master

git push -f origin feature/update_price_rate
Total 0 (delta 0), reused 0 (delta 0)
remote: 
remote: To create a merge request for feature/update_price_rate, visit:
remote:   https://gitlab.hexcloud.cn/histore/supplier-platform/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Fupdate_price_rate
remote: 
To ssh://gitlab.hexcloud.cn:10022/histore/supplier-platform.git
 + d56b776...6fbb4e7 feature/update_price_rate -> feature/update_price_rate (forced update)
  1. 检查远程分支是否回退成功
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值