git版本回退

代码推送到远程仓库之后想回退并且不保留任何历史记录:

  1. 查看版本号信息git log
bash 复制代码
git log
commit version_example1  (HEAD -> dev, origin/dev, origin/HEAD)
Author: xxx <email>
Date: xxx
   XXX注释
   
commit version_example2
Author: xxx <email>
Date: xxx
   XXX注释
  1. 回退git reset --hard 目标版本号
bash 复制代码
git reset --hard version_example2
  1. 推送到远程仓库git push -f
bash 复制代码
git push -f

注:如果是保护分支,需要先解除保护,推送成功之后再加保护!


由于没有及时回退,导致开发组内其他同事代码更新到了之前的变更,这时候该同事执行git pull会报错:

bash 复制代码
s glt pull
hint: Pulling without specifying how to reconcile divergent branches is
hint: discouraged. You can squelch this message by running one of the following
hint: commands sometime before your next pull:
hint:
hint:    git config pull.rebase false  # merge (the default'strategy)
hint:    git config pull.rebase true   # rebase
hint:    git config pull.ff only       # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories, You can also pass --rebase, --no-rebase
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
Already up to date.
bash 复制代码
$ git status
on brancn dev
Your branch is ahead of 'origin/dev' by l commit.
(use "git push" to publish your local commits)

注意保存好开发变更的代码

将该变更撤销即可:

bash 复制代码
git reset --hard HEAD^
相关推荐
CoovallyAIHub21 分钟前
15K Star中文首发!$5部署一个会自我进化的私人Agent——NousResearch开源Hermes Agent
git·架构·github
无限进步_1 小时前
【C++】巧用静态变量与构造函数:一种非常规的求和实现
开发语言·c++·git·算法·leetcode·github·visual studio
降临-max2 小时前
Git 协同开发与冲突解决
笔记·git
高志小鹏鹏3 小时前
告别“修复 bug”:让别人一眼看懂你的 Commit
git·github·代码规范
Rabbit_QL7 小时前
【Git基础】03——Git 撤销与回退:改错了怎么办
大数据·git·elasticsearch
无限进步_7 小时前
【C++&string】寻找字符串中第一个唯一字符:两种经典解法详解
开发语言·c++·git·算法·github·哈希算法·visual studio
木下~learning7 小时前
零基础Git入门:Linux+Gitee实战指南
linux·git·gitee·github·虚拟机·版本控制·ubunt
zh_xuan8 小时前
修改远程仓库名以及和本地工程同步
git
读书札记20228 小时前
Git 配置用户名和邮箱 解决 fatal: unable to auto-detect email address 问题
git
程序员 沐阳9 小时前
Git 二分法精准定位 Bug:从原理到实战,让调试效率起飞
git·elasticsearch·bug