删除文件

回忆一下文件的常见操作,新增文件,修改文件,删除文件等,新增和修改文件都单独讨论过,现在我们来研究一下如何删除文件.

你可能会说删除文件还不简单啊,直接 rm -rf <file> 即可,但是这仅仅是本地文件被删除了,对于 git 来说,文件并没有被删除.

还记得我们开篇介绍git 时就说过,一切操作皆版本 ,对于新增是一个版本,修改也是一个版本,就连删除都是一个版本.

下面让我们看一下 git 中如何删除文件吧!

背景

# 查看当前文件列表
$ ls
file1.txt file2.txt file3.txt newFile.txt test.txt
# 新建待删除文件
$ touch delete.txt
# 再次查看当前文件列表,确保新建文件成功
$ ls
delete.txt file2.txt newFile.txt
file1.txt file3.txt test.txt
# 查看当前文件状态: 新文件 `delete.txt` 还没被跟踪
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed) .DS_Store
delete.txt nothing added to commit but untracked files present (use "git add" to track)
# 添加新文件 `delete.txt`
$ git add delete.txt
# 查看文件状态: 已添加到暂存区,待提交到版本库
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage) new file: delete.txt Untracked files:
(use "git add <file>..." to include in what will be committed) .DS_Store # 提交新文件 `delete.txt`
$ git commit -m "add delete.txt"
[master 7df386a] add delete.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 delete.txt
# 再次查看文件状态: 已经没有新文件 `delete.txt` 的更改信息
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed) .DS_Store nothing added to commit but untracked files present (use "git add" to track)
$

以上操作,我们简单创建 delete.txt 文件,添加(git add)并提交(git commit) 该文件,完成准备工作后,开始删除文件!

# 删除前文件列表
$ ls
delete.txt file2.txt newFile.txt
file1.txt file3.txt test.txt
# 删除刚刚创建的文件 `delete.txt`
$ rm delete.txt
# 删除后文件列表
$ ls
file1.txt file2.txt file3.txt newFile.txt test.txt
# 当前文件状态: `delete.txt` 文件已被删除,且未添加到暂存区
$ 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: delete.txt Untracked files:
(use "git add <file>..." to include in what will be committed) .DS_Store no changes added to commit (use "git add" and/or "git commit -a")
$

本地删除 delete.txt 文件后,再次查看文件状态 git status 发现 git 给了我们两条建议,其中一条 git checkout -- <file> 我们很熟悉,就是丢弃工作区的更改,此时此景下如果丢弃删除操作,相当于撤销删除,难怪说删除也是一个版本呢!

现在我们重点来看第一条建议 git add/rm <file> ,rmremove 单词的缩写,即删除文件.

# 删除文件
$ git rm delete.txt
rm 'delete.txt'
# 查看文件状态: `delete.txt` 文件待提交
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage) deleted: delete.txt Untracked files:
(use "git add <file>..." to include in what will be committed) .DS_Store # 提交文件
$ git commit -m "remove delete.txt"
[master 6298070] remove delete.txt
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 delete.txt
# 再次查看文件状态
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed) .DS_Store nothing added to commit but untracked files present (use "git add" to track)
$

删除文件和添加文件类似,都是一次commit ,本地文件的任何更改都要添加到暂存区,然后提交到版本库.

小结

删除文件和新增文件类似逻辑,git rm 删除文件后,依然需要 git commit 提交版本.

git 入门教程之删除文件的更多相关文章

  1. Git 基础教程 之 删除文件

    ① 手动或命令 rm删除工作区的问价:       git checkout -- readme.txt 可恢复       checkout 实际上是用版本库里的替换工作区的版本 ② 删除了工作区文 ...

  2. git 入门教程之忽略文件

    忽略文件 "并不是所有的牛奶都叫特仑苏",在版本控制系统中也有相似的表达,那就是"并不是所有的文件都需要提交". 有的是因为没必要提交,比如日志文件,系统缓存文 ...

  3. git 入门教程

    git 入门教程之协同开发 前面我们已经介绍过远程仓库的相关概念,不过那时并没有深入探讨,只是讲解了如何创建远程仓库以及推送最新工作成果到远程仓库,实际上远程仓库对于团队协同开发很重要,不仅仅是团队协 ...

  4. 廖雪峰Git入门教程

    廖雪峰Git入门教程  2018-05-24 23:05:11     0     0     0 https://www.liaoxuefeng.com/wiki/00137395163059296 ...

  5. git 常用命令(含删除文件)

    git 常用命令(含删除文件) Git常用操作命令收集: 1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/jquery.git 查看远程仓库: ...

  6. 史上最简单Git入门教程

    一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 工作原理 / 流程: Workspace:工作区Index / Stage:暂存区Repository:仓库区(或本地仓库)Remo ...

  7. Git教程之删除文件(8)

    在Git中,删除也是一个修改操作,我们实战一下,先添加一个新文件test.txt到Git并且提交:

  8. git教程:删除文件

    在Git中,删除也是一个修改操作,我们实战一下,先添加一个新文件test.txt到Git并且提交: $ git add test.txt $ git commit -m "add test. ...

  9. Git入门教程,详解Git文件的四大状态

    大家好,欢迎来到周一git专题. git clone 在上一篇文章当中我们聊了怎么在github当中创建一个属于自己的项目(repository),简称repo.除了建立自己的repo之外,我们更多的 ...

随机推荐

  1. Win下端口占用问题:OSError: [WinError 10013] 以一种访问权限不允许的方式做了一个访问套接字的尝试

    常见问题:https://www.cnblogs.com/dotnetcrazy/p/9192089.html netstat -ano|findstr 8080 如果不计较端口,换个即可 也可以查找 ...

  2. macOS卸载应用不彻底

    总觉得macOS卸载应用时直接移到废纸篓卸载不干净.配置文件根据Unix的习惯应该存放在用户目录下,还需要删除这些文件. ~/Library/Application Support/(应用程序名称) ...

  3. Vue+koa2开发一款全栈小程序(8.图书列表页)

    1.图书列表页获取数据 1.在server/routes/index.js中新增路由 router.get('/booklist',controllers.booklist) 2.在server/co ...

  4. echarts 修改y轴刻度间隔问题

    其中min.max可以自定义可以动态获取数据 yAxis : [ {                        type : 'value',                        axi ...

  5. L1-023 输出GPLT (20 分)

    L1-023 输出GPLT (20 分) 给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按GPLTGPLT....这样的顺序输出,并忽略其它字符.当然,四种字符(不区 ...

  6. NodeJS跨域问题

    const express = require('express'), app = express(), router = express.Router(), bodyParser = require ...

  7. matlab中cumsum函数

    matlab中cumsum函数通常用于计算一个数组各行的累加值.在matlab的命令窗口中输入doc cumsum或者help cumsum即可获得该函数的帮助信息. 格式一:B = cumsum(A ...

  8. VisualSVN服务器的本地搭建和使用

    Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上了,下载地址: http:// ...

  9. DTO/DO等POJO对象的使用场景和 orika-mapper 框架的使用

    对于项目而言, 我们一般会有DAO->Service->Controller分层设计, 这些层次体现了每层的作用, 而层次之间的数据传递对象设计很少被提及, 下面是一个相对完整的数据转换过 ...

  10. Python“函数式编程”中常用的函数

    1.map(func,seq[,seq,...]) 对序列中的每个元素应用函数,python2中map()返回的是列表,python3中返回的是迭代器,可以用list()转换成列表.以下例子为pyth ...