【Git管理的是修改!】Git中的“删除”操作

专业词汇 discard : 删除,丢弃,废除 checkout:检出;通常用来放弃一步操作

实验1 确定删除文件,并且要将版本库中的记录同步删除

创建一个新文件,添加到暂存区并提交。

删除文件,并查询当前Git的工作状态

$ rm test.txt

Administrator@WIN-E2URN8GKKHJ MINGW64 ~/Desktop/learngit (master)
$ ll
total 2
-rw-r--r-- 1 Administrator 197121   0 十月 14 15:44 LICENSE.txt
-rw-r--r-- 1 Administrator 197121 162 十月 14 17:13 readme.txt
-rw-r--r-- 1 Administrator 197121   1 十月 14 17:13 test

Administrator@WIN-E2URN8GKKHJ MINGW64 ~/Desktop/learngit (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    test.txt

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

这里git已经检测到了文件的删除

我们使用命令确定从版本库中删除掉它,然后完成提交,对当前状态进行检测,发现清爽了!

$ git rm test.txt
rm test.txt

Administrator@WIN-E2URN8GKKHJ MINGW64 ~/Desktop/learngit (master)
$ git commit -m "remove test.txt"
[master 1f0fe79] remove test.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 test.txt

Administrator@WIN-E2URN8GKKHJ MINGW64 ~/Desktop/learngit (master)
$ git status
On branch master
nothing to commit, working directory clean

实验2 误删文件,从本地库中恢复文件

另一种情况是删错了,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本:

$ git checkout – test.txt` git checkout其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。


注意

在Git中的操作,都是对于本地的版本库的?

经验分享 程序员 微信小程序 职场和发展