# 查看当前文件列表
$ 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)
$
# 删除前文件列表
$ 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")
$
# 删除文件
$ 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)
$