当前位置: 首页 > java >正文

删除⽂件之git

删除⽂件

在 Git 中,删除也是⼀个修改操作,我们实战⼀下, 如果要删除 file5 ⽂件,怎么搞呢?如果你这样做了:

此时,⼯作区和版本库就不⼀致了,要删⽂件,⽬前除了要删⼯作区的⽂件,还要清除版本库的⽂件。

root@hcss-ecs-a74f:~# cd gitcode
root@hcss-ecs-a74f:~/gitcode# ls
file1  file2  file3  file4  file5  ReadMe
root@hcss-ecs-a74f:~/gitcode# rm file5
root@hcss-ecs-a74f:~/gitcode# ls
file1  file2  file3  file4  ReadMe
root@hcss-ecs-a74f:~/gitcode# git add file5
root@hcss-ecs-a74f:~/gitcode# git status
On branch master
Changes to be committed:(use "git restore --staged <file>..." to unstage)deleted:    file5

⼀般⾛到这⾥,有两种可能:

• 确实要从版本库中删除该⽂件

• 不⼩⼼删错了

对第⼆种情况,很明显误删,需要使⽤ git 来进⾏恢复,很简单,我们刚学过(删除也是修改):

hyb@139-159-150-152:~/gitcode$ git checkout -- file5
hyb@139-159-150-152:~/gitcode$ ls
file1 file2 file3 file4 file5 ReadMe

对于第⼀种情况,很明显是没有删完,我们只删除了⼯作区的⽂件。这时就需要使⽤ git rm 将⽂件从暂存区和⼯作区中删除,并且 commit :

hyb@139-159-150-152:~/gitcode$ git rm file5
rm 'file5'
hyb@139-159-150-152:~/gitcode$ git status
On branch master
Changes to be committed:(use "git restore --staged <file>..." to unstage)deleted: file5
hyb@139-159-150-152:~/gitcode$ git commit -m"deleted file5"
[master 5476bde] deleted file51 file changed, 0 insertions(+), 0 deletions(-)delete mode 100644 file5
hyb@139-159-150-152:~/gitcode$ git status
On branch master
nothing to commit, working tree clean

现在,⽂件就从版本库中被删除了.

完整过程如下:

root@hcss-ecs-a74f:~# cd gitcode
root@hcss-ecs-a74f:~/gitcode# ls
file1  file2  file3  file4  file5  ReadMe
root@hcss-ecs-a74f:~/gitcode# rm file5
root@hcss-ecs-a74f:~/gitcode# ls
file1  file2  file3  file4  ReadMe
root@hcss-ecs-a74f:~/gitcode# git add file5
root@hcss-ecs-a74f:~/gitcode# git status
On branch master
Changes to be committed:(use "git restore --staged <file>..." to unstage)deleted:    file5root@hcss-ecs-a74f:~/gitcode# git commit -m "delete file5"
[master 33b3d8d] delete file51 file changed, 0 insertions(+), 0 deletions(-)delete mode 100644 file5
root@hcss-ecs-a74f:~/gitcode# git status
On branch master
nothing to commit, working tree clean
root@hcss-ecs-a74f:~/gitcode# git rm file4
rm 'file4'
root@hcss-ecs-a74f:~/gitcode# git status
On branch master
Changes to be committed:(use "git restore --staged <file>..." to unstage)deleted:    file4root@hcss-ecs-a74f:~/gitcode# git commit -m "delete file4"
[master 94cdba6] delete file41 file changed, 0 insertions(+), 0 deletions(-)delete mode 100644 file4
http://www.xdnf.cn/news/19791.html

相关文章:

  • 避免侵权!这6个可免费下载字体网站能放心商用
  • 大模型推理加速深度对比:vLLM vs TensorRT-LLM vs ONNX Runtime,谁是生产环境最优解?
  • hot100——第十周
  • linux(cut,sort,uniq ,tr,sed,awk)命令介绍
  • 两个矩形之间的距离 python
  • 互联网大厂Java面试三大回合全解析:从语言特性到性能安全
  • Python数据分析与处理(一):读取不同格式.mat文件的具体方法【超详细】
  • 图解设计模式
  • python - ( js )object对象、json对象、字符串对象的相关方法、数组对象的相关方法、BOM对象、BOM模型中 Navigator 对象
  • Ubuntu中配置JMmeter工具
  • Java 类加载机制(ClassLoader)的必会知识点汇总
  • 当合规成为主旋律,PSP 如何推动链上消费市场迈向新蓝海?
  • MidJourney AI绘图工具测评:支持Discord指令生成图片,含图生图与非商业版权使用功能
  • 零样本视觉模型(DINOv3)
  • 云手机发展:未来的场景变化
  • 【C++】模板(初阶)--- 初步认识模板
  • 三维重建线结构光之重建原理(单线结构光为例)
  • 避坑指南!解决Navicat运行SQL成功但没有表的问题
  • 达梦数据库在大小写不敏感的情况下,如何使查询比较中依旧可以做大小写敏感比较?
  • FFmpeg命令行音视频工具:高效实现格式转换与批量处理,支持音频提取与精准视频剪辑
  • Parasoft C/C++test如何实现开发环境内嵌的安全检测
  • 多工况切换定向:陀螺定向短节 vs 传统陀螺工具,谁的适配性更强?
  • 【单片机day01】
  • 学习React-8-useImmer
  • TDK InvenSense CH201距离传感器
  • 还在从零开发AI应用?这个项目直接给你500个现成方案!!!
  • Autosar之Det模块
  • 智慧工地如何撕掉“高危低效”标签?三大社会效益重构建筑业价值坐标
  • 贝叶斯定理
  • WAF与CDN在网络安全中的协同作用