git恢复文件----文件删除添加到缓存区,但是没提交到本地仓库

条件:

在这里插入图片描述

Administrator@DESKTOP-E9K0JSK MINGW64 /f/workspaces/git/weChat (master)
$ vim demo.txt		#新建demo.txt文件

Administrator@DESKTOP-E9K0JSK MINGW64 /f/workspaces/git/weChat (master)
$ ll		#查看当前文件夹下的文件
total 2
-rw-r--r-- 1 Administrator 197121  24  1月  5 02:12 demo.txt
-rw-r--r-- 1 Administrator 197121 122  1月  5 00:55 good.txt

Administrator@DESKTOP-E9K0JSK MINGW64 /f/workspaces/git/weChat (master)
$ git status		#查看当前状态
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        demo.txt

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

Administrator@DESKTOP-E9K0JSK MINGW64 /f/workspaces/git/weChat (master)
$ git add demo.txt		#将demo.txt添加到缓存区
warning: LF will be replaced by CRLF in demo.txt.
The file will have its original line endings in your working directory

Administrator@DESKTOP-E9K0JSK MINGW64 /f/workspaces/git/weChat (master)
$ git commit -m "创建demo.txt文件" demo.txt		#将demo.txt提交到本地仓库
warning: LF will be replaced by CRLF in demo.txt.
The file will have its original line endings in your working directory
[master 932c463] 创建demo.txt文件
 1 file changed, 4 insertions(+)
 create mode 100644 demo.txt

Administrator@DESKTOP-E9K0JSK MINGW64 /f/workspaces/git/weChat (master)
$ git reflog		#查看项目历史版本
932c463 (HEAD -> master) HEAD@{0}: commit: 创建demo.txt文件
93d703a HEAD@{1}: commit: 删除apple.txt文件
d155333 HEAD@{2}: reset: moving to HEAD
d155333 HEAD@{3}: commit: 创建apple.txt文件
0cfd319 HEAD@{4}: reset: moving to 0cfd319
cc0df73 HEAD@{5}: reset: moving to cc0df73
0cfd319 HEAD@{6}: commit: 删除aaa.txt
cc0df73 HEAD@{7}: commit: 提交aaa.txt
3ada44f HEAD@{8}: reset: moving to 3ada44f
458cdc7 HEAD@{9}: reset: moving to 458cdc7
458cdc7 HEAD@{10}: reset: moving to 458cdc7
3ada44f HEAD@{11}: commit: 提交ffff
21b9c6b HEAD@{12}: commit: 提交eeeee
458cdc7 HEAD@{13}: commit: 提交dddd
0240249 HEAD@{14}: commit: 提交ccc
141db34 HEAD@{15}: commit: 版本提交
b3d2bc3 HEAD@{16}: commit (initial): My first commit.new file goo.txt

Administrator@DESKTOP-E9K0JSK MINGW64 /f/workspaces/git/weChat (master)
$ rm demo.txt		#删除demo.txt文件

Administrator@DESKTOP-E9K0JSK MINGW64 /f/workspaces/git/weChat (master)
$ git status		#查看当前状态
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    demo.txt

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

Administrator@DESKTOP-E9K0JSK MINGW64 /f/workspaces/git/weChat (master)
$ git add demo.txt		#将当前状态提交到缓存区,不提交到本地仓库

Administrator@DESKTOP-E9K0JSK MINGW64 /f/workspaces/git/weChat (master)
$ git status		#查看当前状态
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    demo.txt


Administrator@DESKTOP-E9K0JSK MINGW64 /f/workspaces/git/weChat (master)
$ ll		#查看当前文件夹中的文件
total 1
-rw-r--r-- 1 Administrator 197121 122  1月  5 00:55 good.txt

Administrator@DESKTOP-E9K0JSK MINGW64 /f/workspaces/git/weChat (master)
$ git reset --hard HEAD		#将缓存区和工作区都刷新成指针指向的版本,因为没有提交,所以文件又回来了
HEAD is now at 932c463 创建demo.txt文件

Administrator@DESKTOP-E9K0JSK MINGW64 /f/workspaces/git/weChat (master)
$ ll		#查看当前文件夹文件,demo.txt又回来了
total 2
-rw-r--r-- 1 Administrator 197121  28  1月  5 02:14 demo.txt
-rw-r--r-- 1 Administrator 197121 122  1月  5 00:55 good.txt

Administrator@DESKTOP-E9K0JSK MINGW64 /f/workspaces/git/weChat (master)
$

发布了41 篇原创文章 · 获赞 0 · 访问量 2823

猜你喜欢

转载自blog.csdn.net/weixin_44614772/article/details/103839736