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

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

🔥个人主页:Cx330🌸

❄️个人专栏:《C语言》《LeetCode刷题集》《数据结构-初阶》《C++知识分享》

《优选算法指南-必刷经典100题》《Linux操作系统》:从入门到入魔

《Git深度解析》:版本管理实战全解

🌟心向往之行必能至


🎥Cx330🌸的简介:


目录

前言:

一、Git 实用技巧:版本回退与撤销修改

1.1 版本回退

1.1.1 搭配选项注意事项:

1.1.2 最佳实践

1.2 撤销修改

场景 1:修改仅在工作区,未执行 git add

场景 2:修改过后已执行 git add 到暂存区,但未 commit

场景 3:修改已commit到版本库(未推送到远程)

二. Git 文件删除

方法一:一步一步删除

方法二:git rm [Filename]

总结:


前言:

如果你是刚接触 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 入门指南:从 0 到 1 掌握版本控制

结语:Git 的操作看似多,但核心逻辑很简单:所有操作都围绕「工作区、暂存区、本地仓库」三个区域的同步。建议大家新建一个测试仓库,故意做一些「错误操作」(比如改乱代码、删除文件、提交错误版本),再用本文的方法修复 —— 只有动手实践,才能真正记住这些命令,避免在实际项目中踩坑

Read more

深度盘点:GitHub 上十大必装 Claude Skill,让你的 AI 助手效率提升 4 倍

深度盘点:GitHub 上十大必装 Claude Skill,让你的 AI 助手效率提升 4 倍

深度盘点:GitHub 上十大必装 Claude Skill,让你的 AI 助手效率提升 4 倍 Claude Code 已经很强大,但如果搭配这些精心设计的 Skills,它将变身超级生产力工具。本文为你深度解析 GitHub 上最受欢迎的 10 大 Claude Skills,帮助你找到最适合的配置方案。 引言:为什么 Claude Skills 如此重要? 在 2025-2026 年,Claude Code 生态经历了爆发式增长。Skills 系统的出现,让 Claude 从一个"对话助手"升级为"专业工具"。通过安装不同的 Skills,你可以:

By Ne0inhk
Git 到底是干啥的?零基础小白听完都懂了并且轻松驾驭它

Git 到底是干啥的?零基础小白听完都懂了并且轻松驾驭它

git,通俗的来说就是一种用来多人文件版本合作的工具,但是对一些非程序员的项目小白或者没有程序基础的但是想要入行做程序员的人来说,完完全全理解起来稍微有点困难。这篇文章不像很多文章一样是枯涩的码字教学。现在,我们就用最通俗易懂的方式,让你从零基础理解他,并且使用他。这种教学方法不是把你当白痴的教学方法,反而是让你快速入门深刻理解它,并记住它的教学方法。因为可能说得比较详细,篇幅较长,还得请你耐心的把他看完。 一、git的作用 1、git的版本控制 文件永远不会只有一个版本,这句话我们似乎用亲身经历证明过。你是否有过以下经历👇 📘论文会有“终稿v1、终稿v2、终稿最终版”、 ✍设计稿会有“改版A、改版B、改版C”、 🧺甚至自己写的文章也会来回改十几遍。 🥚更不用说单独只通过一个本地夹操刀一个大型项目了 突然有一天你觉得你的论文、设计稿、文章、项目某一个节点开始脱离了原本的方向或者发生了一些错误,但是你已经对其进行多处修改了,单独再修改不仅费事废经历,还容易发生遗漏。 你或许信誓旦旦的告诉我,你可以这样做。。。👇 论文_最终v1.docx 论文_

By Ne0inhk
Python实现开源AI模型引入及测试全过程

Python实现开源AI模型引入及测试全过程

文章目录 * 摘要 * 1. 引言:开源AI生态系统概述 * 1.1 开源AI的发展现状 * 1.2 技术栈选择 * 1.3 项目目标 * 2. 环境配置与项目初始化 * 2.1 系统要求 * 2.2 创建虚拟环境 * 2.3 依赖管理文件 * 2.4 安装依赖 * 2.5 项目结构 * 3. 模型原理与架构解析 * 3.1 BERT模型原理 * 3.1.1 Transformer编码器架构 * 3.2 Hugging Face Transformers架构 * 4. 数据准备与预处理 * 4.1 数据集选择与加载

By Ne0inhk
SCADA Engine:开源工业级组态引擎,让工业可视化开发更简单

SCADA Engine:开源工业级组态引擎,让工业可视化开发更简单

SCADA Engine:开源工业级组态引擎,让工业可视化开发更简单 前言 在工业互联网和智能制造的浪潮下,SCADA(监控与数据采集)系统成为了工业现场不可或缺的核心工具。然而,传统的组态软件往往存在以下痛点: * 💸 商业软件价格昂贵,中小企业难以承受 * 🔒 闭源不可控,无法根据业务需求灵活定制 * 🎨 界面老旧,用户体验欠佳 * 📱 Web化程度低,难以适应移动化、云化趋势 为了解决这些问题,我开发了 SCADA Engine —— 一个基于 Vue3 和 AntV X6 的开源工业级组态引擎。 项目简介 SCADA Engine 是一个现代化的工业组态编辑器,支持可视化拖拽、实时数据绑定、3D仿真组件和工作流编排。它完全基于 Web 技术栈,可以快速构建工业监控大屏、设备管理界面等应用。 演示地址:http://scada.hn-dc.com/ 图1:SCADA Engine

By Ne0inhk