Git 版本回退与撤销修改

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

一.版本回退

1.概览

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

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

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

|-----------|-----------|-----------|-----|---------|
| ReadMe | 工作区 | 暂存区 | 版本库 | 选项 |
| git world | git world | git world | git | --soft |
| | git world | git | git | --mixed |
| | git | git | git | --hard |

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

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

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

2.回退工作流

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

bash 复制代码
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。

bash 复制代码
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即可)

bash 复制代码
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

bash 复制代码
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

bash 复制代码
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

bash 复制代码
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:   ReadMe

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	file5

git status

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

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

bash 复制代码
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:   ReadMe

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	file5

no changes added to commit (use "git add" and/or "git commit -a")

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

bash 复制代码
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)
	file5

nothing 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

bash 复制代码
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 Third
 1 file changed, 1 insertion(+)

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

bash 复制代码
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)
	file5

nothing added to commit but untracked files present (use "git add" to track)
wujiahao@VM-12-14-ubuntu:~/gitcode$ cat ReadMe
hello Linux
hello git
相关推荐
qq_2518364572 小时前
springboot vue3 开发实现 拼豆管理系统
java·开发语言·ai编程
郑州光合科技余经理3 小时前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
知识分享小能手3 小时前
C++ 学习教程,从入门到精通,C++ 入门知识 — 完整知识点(1)
开发语言·c++·学习
我命由我123453 小时前
Git 推送报错:error: src refspec main does not match any
运维·git·gitee·github·运维开发·学习方法·版本控制
+VX:Fegn08953 小时前
计算机毕业设计|基于springboot + vue旅游管理系统(源码+数据库+文档)
java·开发语言·vue.js·spring boot·课程设计
迪康Defender3 小时前
终端安全实战:如何高效解决企业U盘泄密与管控难题
运维·网络·安全·web安全·终端安全管理
网安小学生(兼顾数据库版)4 小时前
固件+软件供应链:ONEKEY+Mend如何实现全链路安全覆盖
网络·安全·web安全
阿里嘎多学长4 小时前
2026-09-20 GitHub 热点项目精选
开发语言·程序员·github·代码托管
珠海西格电力5 小时前
零碳园区管理系统“智慧大脑”功能对园区运营成本的影响有哪些?
大数据·人工智能·安全·系统架构·能源
发量惊人的中年网工6 小时前
2026年DDoS防护方案怎么选?从攻击响应、清洗位置到成本账单,解析全球高防方案
大数据·网络·安全·ddos