Git 基础操作通关指南:版本回退、撤销修改与文件删除深度解析

🔥个人主页:Cx330🌸
❄️个人专栏:《C语言》《LeetCode刷题集》《数据结构-初阶》《C++知识分享》
《优选算法指南-必刷经典100题》《Linux操作系统》:从入门到入魔
🌟心向往之行必能至
🎥Cx330🌸的简介:

目录
场景 2:修改过后已执行 git add 到暂存区,但未 commit
前言:
如果你是刚接触 Git 的开发者,一定对 “修改、回退、撤销” 这些操作又好奇又犯愁 —— 毕竟版本管理的核心,就藏在这些细节里,本文将聚焦这些进阶操作,拆解其原理、场景与实操细节,帮你在代码出现偏差时,精准、安全地将版本拉回正轨

一、Git 实用技巧:版本回退与撤销修改
在我们日常开发中,难免会遇到代码写错了,代码需要修改等情况
1.1 版本回退
Git支持版本回退与撤销修改等功能,本质上是利用 commit id 与 HEAD master 指针指向的版本
版本回退:git reset 搭配选项
搭配选项:--soft --mixed --hard
1.1.1 搭配选项注意事项:
| 选项 | 作用 | 风险大小 |
| --mixed(默认) | 保留工作区,回退暂存区 | 小 |
| --soft | 保留工作区修改,仅回退仓库和暂存区 | 小 |
| --hard | 彻底回退,工作区、暂存区与仓库保持一致 | 大 |
1.1.2 最佳实践
我们先将文件修改一下,方便我们进行实践:
yhr@VM-24-15-ubuntu:~/gitcode$ ls file1 file2 file3 ReadMe yhr@VM-24-15-ubuntu:~/gitcode$ vim ReadMe yhr@VM-24-15-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 no changes added to commit (use "git add" and/or "git commit -a") yhr@VM-24-15-ubuntu:~/gitcode$ cat ReadMe Hello git Hello Git yhr@VM-24-15-ubuntu:~/gitcode$ git diff ReadMe diff --git a/ReadMe b/ReadMe index 0dec223..e21729a 100644 --- a/ReadMe +++ b/ReadMe @@ -1 +1,2 @@ Hello git +Hello Git 
进行版本回退操作:
yhr@VM-24-15-ubuntu:~/gitcode$ git log --pretty=oneline c5ab24b2ba21f2cb368c3477841504af073c97bc (HEAD -> master) modify ReadMe 746b339d3058dcf6ea9889b3893c12f9be947a4c add 3 file c9919304ee67dc00138cce7659abc2e2d2ea051c add first file yhr@VM-24-15-ubuntu:~/gitcode$ git reset --hard c9919304ee67dc00138cce7659abc2e2d2ea051c HEAD is now at c991930 add first file yhr@VM-24-15-ubuntu:~/gitcode$ la .git ReadMe yhr@VM-24-15-ubuntu:~/gitcode$ cat ReadMe Hello git yhr@VM-24-15-ubuntu:~/gitcode$ git log --pretty=oneline c9919304ee67dc00138cce7659abc2e2d2ea051c (HEAD -> master) add first file 
此时我们就可以发现文件已经回到第一个版本了,文件里面的内容只有Hello git,我们再来看一下日志:git log --pretty=oneline,也可以发现纸打印了一行内容,且HEAD指向的是第一个版本,与我们第一个版本内容相关

那如果我后会修改了呢?我想还回到修改之后的版本,不要第一个版本,那我们该如何做?
很简单,git reset 选项 commit id就可以帮助我们顺利找到之前的版本了,
yhr@VM-24-15-ubuntu:~/gitcode$ git reset --hard c5ab24b2ba21f2cb368c3477841504af073c97bc HEAD is now at c5ab24b modify ReadMe yhr@VM-24-15-ubuntu:~/gitcode$ ls file1 file2 file3 ReadMe yhr@VM-24-15-ubuntu:~/gitcode$ cat ReadMe Hello git Hello Git yhr@VM-24-15-ubuntu:~/gitcode$ git log --pretty=online fatal: invalid --pretty format: online yhr@VM-24-15-ubuntu:~/gitcode$ git log --pretty=oneline c5ab24b2ba21f2cb368c3477841504af073c97bc (HEAD -> master) modify ReadMe 746b339d3058dcf6ea9889b3893c12f9be947a4c add 3 file c9919304ee67dc00138cce7659abc2e2d2ea051c add first file 
如果我们清屏了,或者将系统退出去了,我们发现,我们找不回原来版本的commit id了,那么是不是没有办法了?Git 提供了补救的方法,就是 git relog
yhr@VM-24-15-ubuntu:~/gitcode$ clear yhr@VM-24-15-ubuntu:~/gitcode$ git log commit c9919304ee67dc00138cce7659abc2e2d2ea051c (HEAD -> master) Author: yhr <[email protected]> Date: Wed Dec 3 12:41:06 2025 +0800 add first file yhr@VM-24-15-ubuntu:~/gitcode$ git reflog c991930 (HEAD -> master) HEAD@{0}: reset: moving to c9919304ee67dc00138cce7659abc2e2d2ea051c c5ab24b HEAD@{1}: reset: moving to c5ab24b2ba21f2cb368c3477841504af073c97bc c991930 (HEAD -> master) HEAD@{2}: reset: moving to c9919304ee67dc00138cce7659abc2e2d2ea051c c5ab24b HEAD@{3}: commit: modify ReadMe 746b339 HEAD@{4}: commit: add 3 file c991930 (HEAD -> master) HEAD@{5}: commit (initial): add first file 
我们可以看到第一个版本前面的一串数字:c5ab24b,这串数字是什么呢?其实这是commit id的一部分,我们可以使用git reset --hard c5ab24b进行版本回退到第一个版本,
操作如下:
yhr@VM-24-15-ubuntu:~/gitcode$ git reset --hard c5ab24b HEAD is now at c5ab24b modify ReadMe yhr@VM-24-15-ubuntu:~/gitcode$ git log --pretty=oneline c5ab24b2ba21f2cb368c3477841504af073c97bc (HEAD -> master) modify ReadMe 746b339d3058dcf6ea9889b3893c12f9be947a4c add 3 file c9919304ee67dc00138cce7659abc2e2d2ea051c add first file yhr@VM-24-15-ubuntu:~/gitcode$ cat ReadMe Hello git Hello Git 这时候,我们就可以发现,文件就回到第一个版本了!
但是,这样的结果真的好吗?
不好!在实际开发中,会有非常多的文件,系统可能就会删除,导致commit id找不到,但是如果真的想找到呢?那也无力回天了!
补充知识:
Git 版本的回退速度特别快,因为 Git 在内部有个指向当前分支(此处是master)的HEAD指针,refs/heads/master 文件里保存当前 master 分支的最新的 commit id,当我们在回退版本的时候。Git 仅仅是给refs/heads/master 中存储一个特定的version ,可以简单理解为如下示意图:

1.2 撤销修改
补充知识:
- HEAD 说明:
- 可直接写成 commit id,表⽰指定退回的版本
- HEAD 表示当前版本
- HEAD^ 上一个版本
- HEAD^^ 上上一个版本
- 以此类推…
根据修改所处阶段(工作区 / 暂存区),选择不同的撤销命令:
场景 1:修改仅在工作区,未执行 git add
指令:git checkout -- [Filename]( -- 必须要加上,否则会有其他意思,后面会讲)恢复到最近一次git add或者git commit状态
注意:你当然也可以 vim 编辑器编辑,只要你确保你可以撤销正确,并且源代码不报错,但是不麻烦嘛?
yhr@VM-24-15-ubuntu:~/gitcode$ vim ReadMe yhr@VM-24-15-ubuntu:~/gitcode$ cat ReadMe Hello git Hello Git xxx code #撤销 yhr@VM-24-15-ubuntu:~/gitcode$ git checkout -- ReadMe yhr@VM-24-15-ubuntu:~/gitcode$ cat ReadMe Hello git Hello Git 场景 2:修改过后已执行 git add 到暂存区,但未 commit
修改过后 git add 后还是保存到了暂存区,怎么撤销呢?
#修改一行代码 yhr@VM-24-15-ubuntu:~/gitcode$ vim ReadMe #存放到暂存区 yhr@VM-24-15-ubuntu:~/gitcode$ git add ReadMe #查看 yhr@VM-24-15-ubuntu:~/gitcode$ git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: ReadMe 那我们应该如何撤销呢?很简单,我们回顾一下git reset的选项,如果使用--mixed选项,可以将暂存区的内容回退到指定的版本内容,但工作区文件保持不变,我们再接着用上面场景一的方法解决一下不就好了

流程:先将暂存区修改撤销到工作区,再丢弃工作区修改:
#先将暂存区修改撤销到工作区,--mixed 是默认参数,可以省略 yhr@VM-24-15-ubuntu:~/gitcode$ git reset HEAD ReadMe Unstaged changes after reset: M ReadMe #查看 yhr@VM-24-15-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 no changes added to commit (use "git add" and/or "git commit -a") #回溯到场景一,丢弃到工作区修改 yhr@VM-24-15-ubuntu:~/gitcode$ git checkout -- ReadMe #查看 yhr@VM-24-15-ubuntu:~/gitcode$ git status On branch master nothing to commit, working tree clean yhr@VM-24-15-ubuntu:~/gitcode$ cat ReadMe Hello git Hello Git 我们发现,这样就成功恢复了!
场景 3:修改已commit到版本库(未推送到远程)
直接通过 git reset --hard 回退到上一个版本即可
注意:若已推送(push)到远程仓库,此操作会导致本地与远程版本不一致,需谨慎,这个远程仓库我们在后面会讲的
yhr@VM-24-15-ubuntu:~/gitcode$ vim ReadMe #add + commit yhr@VM-24-15-ubuntu:~/gitcode$ git add ReadMe yhr@VM-24-15-ubuntu:~/gitcode$ git commit -m "modify ReadMe:xxx code" [master faea9c1] modify ReadMe:xxx code 1 file changed, 2 insertions(+) #回退到上一个版本 yhr@VM-24-15-ubuntu:~/gitcode$ git reset --hard HEAD^ HEAD is now at c5ab24b modify ReadMe yhr@VM-24-15-ubuntu:~/gitcode$ git status On branch master nothing to commit, working tree clean yhr@VM-24-15-ubuntu:~/gitcode$ cat ReadMe Hello git Hello Git 二. Git 文件删除
我们都知道删除一个文件可以使用 rm [Filename],但是这样删除,暂存区和版本库里面还是会有的,那么如何将暂存区和版本库里面的对应文件也删除呢?
方法一:一步一步删除
yhr@VM-24-15-ubuntu:~/gitcode$ ls file1 file2 file3 ReadMe #删除修改区的文件 yhr@VM-24-15-ubuntu:~/gitcode$ rm file3 yhr@VM-24-15-ubuntu:~/gitcode$ ls file1 file2 ReadMe #删除文件 yhr@VM-24-15-ubuntu:~/gitcode$ git add file3 yhr@VM-24-15-ubuntu:~/gitcode$ git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) deleted: file3 yhr@VM-24-15-ubuntu:~/gitcode$ git commit -m "delete file3" [master 0e59887] delete file3 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 file3 #查看 yhr@VM-24-15-ubuntu:~/gitcode$ git status On branch master nothing to commit, working tree clean 方法二:git rm [Filename]
yhr@VM-24-15-ubuntu:~/gitcode$ ls file1 file2 ReadMe #删暂存区文件 yhr@VM-24-15-ubuntu:~/gitcode$ git rm file2 rm 'file2' yhr@VM-24-15-ubuntu:~/gitcode$ git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) deleted: file2 #commit提交 yhr@VM-24-15-ubuntu:~/gitcode$ git commit -m "delete file2" [master ebc48f7] delete file2 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 file2总结:
结语:Git 的操作看似多,但核心逻辑很简单:所有操作都围绕「工作区、暂存区、本地仓库」三个区域的同步。建议大家新建一个测试仓库,故意做一些「错误操作」(比如改乱代码、删除文件、提交错误版本),再用本文的方法修复 —— 只有动手实践,才能真正记住这些命令,避免在实际项目中踩坑