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^
相关推荐
洛小豆21 小时前
Git打标签仓库看不到?她说:豆子,你又忘了加 --tags!
git·后端·github
好奇的菜鸟1 天前
理解 Git 命令 `git reset --hard origin/pre`:版本回退的“利刃”与使用禁忌
大数据·git
春生野草1 天前
git的使用
git
今天头发还在吗1 天前
解决 Git 推送冲突:使用 Rebase 整合远程更改
大数据·git·elasticsearch
蓝色空白的博客1 天前
Git代码下拉更新推送操作用法整理
git
风也温柔☆1 天前
IDEA/WebStorm 切换分支(超简单)
git·intellij-idea·webstorm·切换分支·checkout
Jammingpro2 天前
【Git版本控制】Git初识、安装、仓库初始化与仓库配置(含git init、git config与配置无法取消问题)
java·git·elasticsearch
shark2 天前
无需放弃变更、关闭占用程序!用暂存区和 git底层命令实现 Git 变更备份
git·shell·自动化运维
_poplar_2 天前
15 【C++11 新特性】统一的列表初始化和变量类型推导
开发语言·数据结构·c++·git·算法
北城笑笑2 天前
Git 10 ,使用 SSH 提升 Git 操作速度实践指南( Git 拉取推送响应慢 )
前端·git·ssh