git 不小心删除本地文件恢复

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

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

[xxx@xxx static_files]$ git status
# On branch master
nothing to commit (working directory clean)
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

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

xxx@xxx static_files]$ ls
abbr_data  breakfast_data  room_type_data
 
  • 1
  • 2
  • 1
  • 2

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

[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
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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

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

[xxx@xxx static_files]$ git checkout -- abbr_data
error: pathspec 'static_files/abbr_data' did not match any file(s) known to git.
 
  • 1
  • 2
  • 1
  • 2

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

[xxx@xxx static_files]$ git reset HEAD abbr_data
Unstaged changes after reset:
M   static_files/abbr_data
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

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

[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")
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

再checkout一把

[xxx@xxx static_files]$ git checkout -- abbr_data
[xxx@xxx static_files]$
 
  • 1
  • 2
  • 1
  • 2

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

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

[xxx@xxx static_files]$ ls
abbr_data  breakfast_data  room_type_data
### 如何用 Git 恢复本地意外删除的文件 如果在开发过程中小心删除本地文件,可以通过 `git` 的功能来恢复这些被误删的文件。以下是具体的方法: #### 方法一:通过 `git checkout` 命令恢复最新版本的文件 当文件已经被删除但仍存在于某个提交记录中时,可以使用以下命令将其恢复到最新的状态: ```bash git checkout -- <file> ``` 这条命令会将指定文件 `<file>` 从当前分支的索引(index)中取出并覆盖工作目录中的内容[^2]。 #### 方法二:从未推送的提交历史中恢复文件 如果文件曾经被提交过但后来又被删除,则可以从仓库的历史记录中提取出来。假设你知道要恢复的具体路径或者名称,可以运行如下命令: ```bash git rev-list -n 1 HEAD -- <file> | xargs git checkout ``` 此方法适用于更复杂的情况,比如确定最后一次修改的时间点或想精确控制恢复范围[^4]。 #### 方法三:利用缓存区 (`--cached`) 进行处理 如果你之前只是移除了跟踪列表里的条目而没有真正物理上清除磁盘上的副本(即执行的是类似于这样的操作:`git rm --cached file1.txt`),那么只需简单地重新添加回来即可完成修复过程: ```bash git add <file> ``` 注意,在实际应用上述任何一种方式前最好先确认目标对象确实存在对应的快照数据;另外还需留意是否存在未解决冲突等问题影响最终效果[^1]。 ```python # 示例 Python 脚本用于演示逻辑流程而非直接参与恢复动作 def restore_deleted_files(file_name): import subprocess try: result =subprocess.run(['git', 'checkout', '--', file_name], check=True) return f"File {file_name} restored successfully." except Exception as e: return str(e) print(restore_deleted_files('example.txt')) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值