前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >git 删除文件后如何恢复[通俗易懂]

git 删除文件后如何恢复[通俗易懂]

作者头像
全栈程序员站长
发布2022-09-23 10:16:33
2.4K0
发布2022-09-23 10:16:33
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君

项目github地址:bitcarmanlee easy-algorithm-interview-and-practice 欢迎大家star,留言,一起学习进步

有时候不小心在git中rm了文件。怎么恢复呢?别急,咱们一步步来。

首先git status一把,看看此时工作区的状态

代码语言:javascript
复制
[xxx@xxx static_files]$ git status
# On branch master
nothing to commit (working directory clean)

可见此时没有任何修改的内容。 再看看具体有什么

代码语言:javascript
复制
xxx@xxx static_files]$ ls
abbr_data  breakfast_data  room_type_data

此时总计有三个文件。OK,让我们干掉其中一个

代码语言:javascript
复制
[xxx@xxx static_files]$ git rm abbr_data
rm 'static_files/abbr_data'
[xxx@xxx static_files]$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	deleted:    abbr_data
#
[xxx@xxx static_files]$ ls
breakfast_data  room_type_data

此时工作区的文件就只剩两个了,abbr_data这个文件,已经被我们干掉。

如果我们想要恢复,怎么办呢?

代码语言:javascript
复制
[xxx@xxx static_files]$ git checkout -- abbr_data
error: pathspec 'static_files/abbr_data' did not match any file(s) known to git.

直接checkout,是不行的。 那怎么办呢?其实在git status中,已经告诉我们怎么办了。

代码语言:javascript
复制
[xxx@xxx static_files]$ git reset HEAD abbr_data
Unstaged changes after reset:
M	static_files/abbr_data

用reset命令,先将abbr_data这个文件找回来。

代码语言:javascript
复制
[xxx@xxx static_files]$ git status
# On branch master
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	deleted:    abbr_data
#
no changes added to commit (use "git add" and/or "git commit -a")

再checkout一把

代码语言:javascript
复制
[xxx@xxx static_files]$ git checkout -- abbr_data
[xxx@xxx static_files]$

看到checkout以后没有任何提示,这事就成了。因为git的哲学跟unix的哲学一样,没消息就是最好的消息。。。

再ls一下,果然,abbr_data找回来了。

代码语言:javascript
复制
[xxx@xxx static_files]$ ls
abbr_data  breakfast_data  room_type_data

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/172143.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档