当前位置: 首页 > news >正文

Git 版本回退与撤销修改

作为版本控制管理器,Git应当具备版本回退等一系列功能——它的应用场景也很常见,当你在工作区开发时,忽然发现:怎么我这版本写的代码还不如上一版本好?这时,版本回退功能就派上用场了。

一.版本回退

1.概览

首先我们先明确版本回退的指令

git reset [--soft]|--mixed|[--hard]  [HEAD]

我们来一一解释这几个选项对应的作用。

ReadMe工作区暂存区版本库选项
git worldgit worldgit worldgit--soft
git worldgitgit--mixed
gitgitgit--hard

1.--soft:仅回退版本库的内容

2.--mixed:回退暂存区和版本库内容,是默认选项

3.--hard:将所有区域内容全部回退,有可能会覆盖当前工作区的内容,建议谨慎使用。

2.回退工作流

1.git log——查看日志。若要执行回退,首先要明确回退到哪个版本。

wujiahao@VM-12-14-ubuntu:~/gitcode$ git log --pretty=oneline
18812885f304b7ba2baa67a109a2762ebb6f85c3 (HEAD -> master) add some files
f5aa7498c931ae9bc6cf0edfa400f7ceb28816a9 add first word

2.git reset ——执行回退,选项后面跟上commitID即可。例如这里回退到 add some files。

wujiahao@VM-12-14-ubuntu:~/gitcode$ git reset --hard 18812885f304b7ba2baa67a109a2762ebb6f85c3
HEAD is now at 1881288 add some files#现在的效果
wujiahao@VM-12-14-ubuntu:~/gitcode$ cat ReadMe
hello Linux
hello git

3.git reflog——如果后悔回退怎么办?可以查找本地所有reset操作的日志,回退到其他想要的版本(继续执行git reset即可)

wujiahao@VM-12-14-ubuntu:~/gitcode$ git reflog
1881288 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
d6a7559 HEAD@{1}: commit: Modify ReadMe Third
1881288 (HEAD -> master) HEAD@{2}: reset: moving to 18812885f304b7ba2baa67a109a2762ebb6f85c3
1881288 (HEAD -> master) HEAD@{3}: commit: add some files
f5aa749 HEAD@{4}: commit (initial): add first word

3.回退的原理

我们在执行版本回退时,会发现其执行速度非常快。那么它的原理是什么呢?

根据commitID直接修改HEAD指针指向的git对象

二.撤销修改

1.概览

撤销修改的情况可以大概分为以下几类:

情况执行操作
仅修改工作区git checkout -- filename
执行了add,但没有commit到版本库

1.git reset --hard filename

2.git reset --mixed filename

git checkout -- filename

已经commit到版本库,但没有执行push到远程仓库git reset --hard

接下来一一进行详解。

2.详解

1.仅修改工作区

例如这里我们对ReadMe写入xxx code

wujiahao@VM-12-14-ubuntu:~/gitcode$ vim ReadMe
wujiahao@VM-12-14-ubuntu:~/gitcode$ cat ReadMe
hello Linux
hello git
xxx code

怎样回退到之前的版本?我们当然可以选择用vim编辑器等修改,但是修改的部分越多越复杂,这种方法越不实用。因此我们使用以下方法:

git checkout -- filename

wujiahao@VM-12-14-ubuntu:~/gitcode$ git checkout -- ReadMe
wujiahao@VM-12-14-ubuntu:~/gitcode$ cat ReadMe
hello Linux
hello git

2.执行了add,但没有commit到版本库

我们写入this is the second change并执行add

wujiahao@VM-12-14-ubuntu:~/gitcode$ vim ReadMe
wujiahao@VM-12-14-ubuntu:~/gitcode$ cat ReadMe
hello Linux
hello git
this is the second change
wujiahao@VM-12-14-ubuntu:~/gitcode$ git add ReadMe
wujiahao@VM-12-14-ubuntu:~/gitcode$ git status
On branch master
Changes to be committed:(use "git restore --staged <file>..." to unstage)modified:   ReadMeUntracked files:(use "git add <file>..." to include in what will be committed)file5

git status

查看当前git状态,发现暂存区有文件被修改(ReadMe)

首先这里有两种解决办法,一种直接reset –hard,另一种是reset –mixed(默认选项),默认回退版本库和暂存区,相当于回到了第一种情况。在这里介绍第二种用法

wujiahao@VM-12-14-ubuntu:~/gitcode$ git reset HEAD ReadMe
Unstaged changes after reset:
M	ReadMe
wujiahao@VM-12-14-ubuntu:~/gitcode$ git status
On branch master
Changes not staged for commit:(use "git add <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory)modified:   ReadMeUntracked files:(use "git add <file>..." to include in what will be committed)file5no changes added to commit (use "git add" and/or "git commit -a")

然后再使用和情况1的指令即可

wujiahao@VM-12-14-ubuntu:~/gitcode$ git checkout -- ReadMe
wujiahao@VM-12-14-ubuntu:~/gitcode$ git status
On branch master
Untracked files:(use "git add <file>..." to include in what will be committed)file5nothing added to commit but untracked files present (use "git add" to track)
wujiahao@VM-12-14-ubuntu:~/gitcode$ cat ReadMe
hello Linux
hello git

3.已经commit到版本库,但没有执行push到远程仓库

我们加入 this is the third change,并且add和commit

wujiahao@VM-12-14-ubuntu:~/gitcode$ cat ReadMe
hello Linux
hello git
wujiahao@VM-12-14-ubuntu:~/gitcode$ vim ReadMe
wujiahao@VM-12-14-ubuntu:~/gitcode$ cat ReadMe
hello Linux
hello git
this is the third change
wujiahao@VM-12-14-ubuntu:~/gitcode$ git add ReadMe
wujiahao@VM-12-14-ubuntu:~/gitcode$ git commit -m "Modify ReadMe Third "
[master d6a7559] Modify ReadMe Third1 file changed, 1 insertion(+)

只需要用reset的hard选项即可,HEAD表示当前版本,那么上个版本就是HEAD^

wujiahao@VM-12-14-ubuntu:~/gitcode$ git reset --hard HEAD^
HEAD is now at 1881288 add some files
wujiahao@VM-12-14-ubuntu:~/gitcode$ git status
On branch master
Untracked files:(use "git add <file>..." to include in what will be committed)file5nothing added to commit but untracked files present (use "git add" to track)
wujiahao@VM-12-14-ubuntu:~/gitcode$ cat ReadMe
hello Linux
hello git


文章转载自:

http://Ch2l49Ak.zLxrg.cn
http://PQzOa03A.zLxrg.cn
http://57WOEfZz.zLxrg.cn
http://Im2BtN8L.zLxrg.cn
http://SODKCjzu.zLxrg.cn
http://1AartF1r.zLxrg.cn
http://8ww50Fhj.zLxrg.cn
http://Bb5ZAiEM.zLxrg.cn
http://jHMeC4Rv.zLxrg.cn
http://PvKerATa.zLxrg.cn
http://U8KnlawG.zLxrg.cn
http://YAnMcTei.zLxrg.cn
http://PvQ67Sjy.zLxrg.cn
http://TTG3BYqA.zLxrg.cn
http://RI42NURF.zLxrg.cn
http://9hIDo8v1.zLxrg.cn
http://T4R5P4B9.zLxrg.cn
http://IN0HLrtS.zLxrg.cn
http://piFFpiXE.zLxrg.cn
http://P6NO4Bdl.zLxrg.cn
http://XwhmM51j.zLxrg.cn
http://VAr3sYvo.zLxrg.cn
http://A5RsAEkU.zLxrg.cn
http://Lu1FVOX8.zLxrg.cn
http://xJBuyExR.zLxrg.cn
http://iyfOS2Gw.zLxrg.cn
http://CsVQYKQH.zLxrg.cn
http://o3JMoVY5.zLxrg.cn
http://D2CRS8PY.zLxrg.cn
http://UUcnrGQr.zLxrg.cn
http://www.dtcms.com/a/382237.html

相关文章:

  • Tcpdump: The Basics Tcpdump 基础
  • 智慧物联网水利大数据平台建设方案PPT(70页)
  • 字典树初步
  • GitHub 热榜项目 - 日榜(2025-09-13)
  • 18、决策树与集成学习 - 从单一智慧到群体决策
  • 基于 Spring 的策略模式框架,用于根据不同的类的标识获取对应的处理器实例
  • Python:OpenCV 教程——从传统视觉到深度学习:YOLOv8 与 OpenCV DNN 模块协同实现工业缺陷检测
  • UTC时间戳转换
  • 【Unity进阶】Unity发布PC端,隐藏并自定义默认标题栏
  • 【Qt】编写Qt自定义Ui控件步骤
  • HTTP 状态码背后的逻辑:从请求到响应的完整流程解析(含完整流程图)
  • 如何规划活动宣传软文的发布节奏?
  • 什么是NTP?
  • n8n工作流平台入门学习指南
  • JVM 四大晋升机制
  • ES——(一)基本概念
  • 算法提升之树形数据结构
  • 使用 OpenTelemetry 从你的日志中获取更多信息
  • Java中IntStream的详细用法及典型案例
  • Python ast模块(Abstract Syntax Trees,抽象语法树)介绍及使用
  • UFO²:微软推出的新一代桌面 Agent 操作系统,深度整合 Windows 与智能自动化
  • 嵌入式ARM SOC开发中文专题分享一:ARM SOC外围资源介绍
  • Java 大视界 -- 基于 Java 的大数据分布式计算在气象灾害数值模拟与预警中的应用
  • Python项目全面打包指南:从EXE到绿色软件包
  • C语言---运算符
  • 什么是包装类
  • 59.[前端开发-Vue3]Day01-Vue初体验-MVVM-模板语法-常用指令
  • 1.13 Memory Profiler Package - Unity Objects(unity对象页签)
  • Nginx 请求到达nginx,但是到不了业务服?报错408
  • 若依分库分表,在admin模块可查询子库,在API模块无法查询