my-gitbook
Blog
  • 👨‍💻简介
  • 入门教程
    • markdown 入门教程
    • git 入门教程
      • 初识 git
        • 安装 git
        • 配置 git
      • 实战 git
        • 本地仓库
        • 版本管理
        • 回到过去
        • 基本概念
        • 版本控制
        • 撤销更改
        • 删除文件
        • 远程仓库
        • 分支管理
          • 分支总览
          • 冲突合并
          • 分支策略
          • 紧急修复
          • 变基合并
          • 协同开发
          • 本地和远程仓库的本质
        • 里程碑式标签
      • 私人定制
        • 忽略文件
        • 同步推送多Git仓库
      • 搭建私服
      • 扩展工具
        • git bash 常见命令
        • git bash 朋友圈
        • git bash 扩展tree命令
        • git bash 扩展命令(上)
        • git bash 扩展命令(中)
        • git bash 扩展命令(下)
        • git submodule 父子模块
      • 沙海拾贝
        • 知识速查
        • 备忘录[译]
    • github 入门教程
      • github 访问速度太慢
      • github 徽章从何而来
    • gitbook 入门教程
      • 准备阶段
        • 前置知识
        • 环境要求
      • 快速体验
        • gitbook-cli 命令行操作
        • gitbook-editor 编辑器操作
        • gitbook.com 官网操作
      • 高级进阶
        • 插件介绍
          • 实用插件
          • 主题插件
          • 开发插件
        • 导出电子书
        • 发布电子书
      • 公众号引流
      • 优化搜索
      • 常见问题
        • 热加载失败治标之法
        • 初始化默认折叠效果
      • 参考更多
  • 编程笔记
    • java 学习笔记
    • go 学习笔记
    • php 学习笔记
    • python 学习笔记
      • 入门教程
        • 00-ubuntu安装
        • 01-Linux基础
        • 操作系统的发展史(科普章节)
      • Django教程
        • Django 学习笔记
    • Linux 学习笔记
      • Linux基础
      • Linux全栈运维
  • 兴趣开发
    • 从零开始搭建服务器
      • 从登录服务器开始
      • 更加优雅地部署项目
      • 跟着官方体验 Docker
      • 快速尝鲜基于 Docker 部署项目
      • 利用阿里云 OSS部署静态网站
      • 免费实现https访问网站
      • webhook
    • 运维部署
      • docker 环境
      • docker 操作
      • docker compose
      • docker + nginx
      • docker + blog
      • docker + resume
      • docker + gitbook
      • docker + Bark
      • docker + py12306
      • docker + mojo-Weixin
      • docker + wechat-work-message-push-go
      • docker + email
      • docker + mysql
      • docker + springboot
      • docker + redis
      • docker + gogs
      • docker + gitlab
      • docker + gitea
      • docker + crawlab
    • 工具资源
      • 给 mac 装个 vmware 虚拟机
        • 给 mac 虚拟机装个 windows
        • 给 mac 虚拟机装个 centos
        • 给 mac 虚拟机装个 ubuntu
      • 给 windows 装个 vmware 虚拟机
        • 给 windows 虚拟机装个 mac
        • 给 windows 虚拟机装个 windows
        • 给 windows 虚拟机装个 centos
        • 给 windows 虚拟机装个 ubuntu
      • 公众号历史文章备份
        • Ryen
        • 苏生不惑
        • Python技术
      • 学会开发专属 chrome 插件
      • 攻克 12306 前端加密算法
    • 如何写作
      • markdown 和富文本
      • markdown 转富文本
      • csv 半手动数据统计
      • js 半手动数据统计
      • 简述优选文章统计
      • 快速上手Mermaid流程图
      • 常见问题
  • 读书观感
    • 大数据浪潮之巅:新技术商业制胜之道
  • 程序人生
    • 待完成清单
      • yarn快速入门
      • gulp快速入门
      • nvm快速入门
      • lodash快速入门
      • regexper快速入门
      • sublime快速入门
      • github 快速入门
      • svn 快速入门
      • java 快速入门
      • mysql 快速入门
      • maven 快速入门
      • tomcat 快速入门
      • gitignore 快速入门
      • android studio 快速入门
      • npm 快速入门
      • nginx 快速入门
    • 其他
      • 重装开发环境
      • 钉钉签到提醒
      • 倒计时提醒
      • 关注网站
      • 转型之路
      • 历史统计
      • 关于作者
      • 捐赠支持
由 GitBook 提供支持
在本页
  • 背景
  • 小结

这有帮助吗?

在GitHub上编辑
  1. 入门教程
  2. git 入门教程
  3. 实战 git

撤销更改

相信你已经了解了 git 的基本概念,也清楚了工作区,暂存区和版本库的关系,现在让我们用所学的知识继解决实际问题吧!

背景

正常看得见的目录是我们最为熟悉的工作区,在工作中不可能总是100%的精力,难免会犯错,尤其是下午犯困,晚上加班更是如此.下面列举了常见的一些场景

  • 场景一: 工作区出现意外更改且尚未添加到暂存区

北京时间现在是晚上10点钟,你正在赶制一份工作报告,尽管心中一万个不愿意,还是不得不做.

开始模拟意外更改前,先查看一下 test.txt 文件相关信息:

# 列出当前目录的文件
$ ls
file1.txt   file2.txt   file3.txt   newFile.txt test.txt
# 查看 `test.txt` 文件内容
$ cat test.txt
git test
git init
git diff
understand how git control version
how git work
git tracks changes of files
# 查看 `test.txt` 文件状态
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   test.txt

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

    .DS_Store

no changes added to commit (use "git add" and/or "git commit -a")
# 查看 `test.txt` 文件差异
$ git diff 
diff --git a/test.txt b/test.txt
index d31bdd2..56c76b7 100644
--- a/test.txt
+++ b/test.txt
@@ -3,4 +3,4 @@ git init
 git diff
 understand how git control version
 how git work
-git tracks changes
+git tracks changes of files
$ 

还记得在上一节中我们讲解 git 版本控制的到底是什么,为了证明 git 管理的是更改而不是文件本身,我们特意在第二次更改时没有添加到暂存区,现在我们先把这个遗留问题解决掉.

# 工作区更改添加到暂存区
$ git add test.txt
# 暂存区内容提交到版本没哭
$ git commit -m "git tracks changes of files"
[master b7bda05] git tracks changes of files
 1 file changed, 1 insertion(+), 1 deletion(-)
# 查看文件状态
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .DS_Store

nothing added to commit but untracked files present (use "git add" to track)
$ 

现在正在加班加点干活,一不小心将心中的不满表露出来了,于是有了下面的内容:

# 意外更改正是这么犯傻的一句话
$ echo "My stupid boss still prefers svn" >> test.txt
# 当前文件内容
$ cat test.txt
git test
git init
git diff
understand how git control version
how git work
git tracks changes of files
My stupid boss still prefers svn
$ 

虽然强打精神,可还是很困,于是打算喝杯咖啡提提神,猛然发现 stupid boss 可能会让你丢掉这个月的奖金!

暗自庆幸,咖啡果然是个好东西,既然发现了问题,那就事不宜迟赶紧修复,因为不适宜的话正是 stupid boss ,所以你完全可以手动删除,但是假如你说了一大堆不合适的话,或者复制粘贴时弄错了,这就不是删除一两行那么简单了!

既然手动解决比较麻烦,那git 有没有什么好方法来解决这类问题呢?在寻求git 帮助前,首先再看一下当前文件状态(git status).正所谓"知己知彼方能百战百胜",还是看一眼吧!

# 查看文件状态
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   test.txt

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

    .DS_Store

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

git 不负众望,果然给了我们希望,(use "git checkout -- <file>..." to discard changes in working directory) 这句话的告诉我们可以丢弃工作区的更改!

脑海中在快速回忆一下工作区,暂存区,版本库三者之间的关系,其实git checkout -- <file> 命令的意思是用暂存区的内容替换掉工作区内容,因此也就是丢弃掉工作区的更改了.

事不宜迟,运行 git checkout -- <file> 命令试试看吧:

# 丢弃工作区的更改
$ git checkout -- test.txt
# 查看文件内容: My stupid boss still prefers svn 终于不见了
$ cat test.txt
git test
git init
git diff
understand how git control version
how git work
git tracks changes of files
# 查看文件状态
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .DS_Store

nothing added to commit but untracked files present (use "git add" to track)
$ 

一顿操作猛如虎,撤销掉意外更改,回到上一次版本控制状态,世界如此美好...

注意: git checkout -- <file> 中的 -- 至关重要,没有它就是切换分支了!

  • 场景二: 工作区出现意外更改且已经添加到暂存区,但尚未提交到版本库

时间一分一秒过去了,转眼间已经11点了,假设你不但写了一些胡话,还添加到暂存区了(git add).可想而知,这次意外比场景一要糟糕.

# 模拟正常提交(不然岂不是从场景一到场景二你什么都没做,那还能叫做赶制工作报告吗?!)
$ echo "someone prefers svn,but i don't care it" >> test.txt
$ cat test.txt
git test
git init
git diff
understand how git control version
how git work
git tracks changes of files
someone prefers svn,but i don't care it
$ git add test.txt
$ git commit -m "normal commit"
[master ab1cbd2] normal commit
 1 file changed, 1 insertion(+)

# 意外更改的前夕 
$ cat test.txt
git test
git init
git diff
understand how git control version
how git work
git tracks changes of files
someone prefers svn,but i don't care it

# 意外更改内容: my teammate is stupid too.
$ echo "my teammate is stupid too." >> test.txt
$ cat test.txt
git test
git init
git diff
understand how git control version
how git work
git tracks changes of files
someone prefers svn,but i don't care it
my teammate is stupid too.

# 意外操作: 将意外更改内容提交到暂存区
$ git add test.txt 

不过庆幸的是,在提交到版本库(git commit)之前及时发现问题,还是看一下现在的文件状态(git status)吧!

# 查看文件状态: 救命稻草 (use "git reset HEAD <file>..." to unstage)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   test.txt

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

    .DS_Store

$ 

git 同样告诉我们,可以使用 git reset HEAD <file> 命令撤销暂存区更改.

其实 git reset HEAD <file> 命令是用版本库的内容替换掉暂存区的内容,也就是说原来暂存区的内容已被丢弃了!

所以说这个命令并不会影响工作区内容,不如我们现在再看一眼工作区内容,方便执行 git reset HEAD <file> 命令后证实我们的结论.

# 查看文件内容: my teammate is stupid too.
$ cat test.txt
git test
git init
git diff
understand how git control version
how git work
git tracks changes of files
someone prefers svn,but i don't care it
my teammate is stupid too.
$ 

迫不及待执行 git reset HEAD <file> 命令,先睹为快!

# 救命稻草: 版本库内容替换掉暂存区内容
$ git reset HEAD test.txt
Unstaged changes after reset:
M   test.txt

# 效果: 目标文件已修改但未添加到暂存区
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   test.txt

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

    .DS_Store

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

# 目标文件内容: 仍然保持不变
$ cat test.txt
git test
git init
git diff
understand how git control version
how git work
git tracks changes of files
someone prefers svn,but i don't care it
my teammate is stupid too.
$ 

现在场景二已经退化成场景一了,目标文件发生意外更改但还没添加到暂存区,如何撤销工作区更改,请参考场景一方法.

提示: git checkout -- test.txt

  • 场景三: 工作区出现意外更改不仅已添加到暂存区,还已提交到版本库,但尚未推送到远程仓库

时间不紧不慢地已经到凌晨了,困意越来越浓,洋洋洒洒写下几千字的工作报告,总算是写完了,添加到暂存区(git add),提交到版本库(git commit)一气呵成,等等,好像有什么不对劲,难免会犯糊涂,这不又发生意外了!

# 衔接场景二
$ cat test.txt
git test
git init
git diff
understand how git control version
how git work
git tracks changes of files
someone prefers svn,but i don't care it

# 正常提交一
$ echo "i love working,work makes me happy" >> test.txt
$ git add test.txt
$ git commit -m "encourage myself"
[master a44cf7a] encourage myself
 1 file changed, 1 insertion(+)

# 正常提交二
$ echo "fix 110 bugs,so happy" >> test.txt
$ git add test.txt
$ git commit -m "fix bugs"
[master c66399d] fix bugs
 1 file changed, 1 insertion(+)
sunpodeMacBook-Pro:demo sunpo$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .DS_Store

nothing added to commit but untracked files present (use "git add" to track)

# 意外更改: hate to work overtime
$ echo "hate to work overtime" >> test.txt
$ git add test.txt
$ git commit -m "test.txt"
[master c965724] test.txt
 1 file changed, 1 insertion(+)
 $ 

天妒英才,加班加点做事情,本想赢得老板的赏识,没想到最后一句话"hate to work overtime"让所有的努力都付之一炬,怎么办?

死马当活马医,还是照例看看git status 能提供什么建议吧!

$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .DS_Store

nothing added to commit but untracked files present (use "git add" to track)
$ 

没有提供任何意见能帮助我们撤销意外更改,先别慌,容我深思三秒钟...

既然意外更改已经提交到版本库,那么应该用什么内容替换版本库内容呢?有了,既然最新版本库不可用,那上一个版本库内容可用的啊,完全可以用上一个版本库内容替换最新版本库内容,真乃"天生我材必有用"!

# 当前文件内容: 闯祸的"hate to work overtime"
$ cat test.txt
git test
git init
git diff
understand how git control version
how git work
git tracks changes of files
someone prefers svn,but i don't care it
i love working,work makes me happy
fix 110 bugs,so happy
hate to work overtime

# 版本回退: 回到过去假装什么都没发生过
$ git reset --hard HEAD^
HEAD is now at c66399d fix bugs
sunpodeMacBook-Pro:demo sunpo$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .DS_Store

nothing added to commit but untracked files present (use "git add" to track)

# 岁月静好,一切似乎都没发生过
$ cat test.txt
git test
git init
git diff
understand how git control version
how git work
git tracks changes of files
someone prefers svn,but i don't care it
i love working,work makes me happy
fix 110 bugs,so happy

# 当前文件状态
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .DS_Store

nothing added to commit but untracked files present (use "git add" to track)
$ 

提示: git reset --hard HEAD^

  • 场景四: 工作区出现意外更改不仅已添加到暂存区,还提交到版本库,还已推送到远程仓库

场景一到场景三都是本地仓库,所有的文件更改只能本机访问,小伙伴也好,上级领导也罢都无法查看到你本地更改,但是一旦你推送到远程仓库了,那么其他人就能查看你的更改了!

正常的提交更改还好,怕就怕这种"stupid boss"被领导看到就不好了,那应该怎么办?暂时还是自求多福吧!

小结

  • 丢弃工作区更改: git checkout -- <file>

  • 丢弃暂存区更改: git reset HEAD <file>

  • 丢弃本地版本库更改: git reset --hard HEAD^

  • 丢弃远程版本库更改: 自求多福

上一页版本控制下一页删除文件

最后更新于1年前

这有帮助吗?

详情请参考,时空穿越之旅就是这么方便哈!

回到过去