git
  • 安装教程
  • 本地仓库
    • 工作区与版本库
    • 版本管理
      • 创建版本库
      • 提交修改
      • 撤消修改
      • 删除文件
    • 分支管理
      • 创建分支
      • 合并分支
      • 删除分支
    • 标签管理
      • 创建标签
      • 删除标签
  • 远程仓库
    • 初始化远程库
    • 克隆远程库
    • 多人协作
    • 远程分支
    • 远程标签
    • 关于origin
    • 关于upstream
  • 关于config
    • 关于name与email
    • window上的凭证
  • git常用命令
  • github的使用
  • 容器跑gogs
  • 项目版本管理
    • GitFlow
Powered by GitBook
On this page

Was this helpful?

  1. 本地仓库
  2. 版本管理

删除文件

在git中,删除也是一个修改操作, 我们来实战一下。首先,添加一个test.txt文件并向git提交

$ git add test.txt
$ git commit -m "initialize test.txt"
[master e175c41] initialize test.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt

然后,我们在shell下用rm直接删除该文件

$ rm -f test.txt

这个时候,Git知道你删除了文件,因此,工作区和版本库就不一致了,git status命令会立刻告诉你哪些文件被删除了:

$ 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 rm删掉,并且git commit:

$ git rm test.txt
rm 'test.txt'
$ git commit -m "delete test.txt"
[master b41a08d] delete test.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 test.txt

现在,文件就从版本库中删除了

另一种情况是误删了文件,因为版本库中还有,所以我们能把它最新的版本从版本库中恢复出来

$ git checkout -- test.txt
Previous撤消修改Next分支管理

Last updated 5 years ago

Was this helpful?