[toc]
chapter(五)
5.1 如何删除文件?
上一章说的是撤销修改,这一章就谈谈如何删除文件。 实践是检验真理的唯一标准,话不多说,直接干:
toto@pc:~/code/testGit$ vim deleteFile.txt
toto@pc:~/code/testGit$ cat deleteFile.txt
This is a test deleted file!
toto@pc:~/code/testGit$ git add deleteFile.txt
toto@pc:~/code/testGit$ git commit -m "test deleted file"
[master 190efbd] test deleted file
1 file changed, 1 insertion(+)
create mode 100644 deleteFile.txt
toto@pc:~/code/testGit$ git status
On branch master
nothing to commit, working tree clean
toto@pc:~/code/testGit$ ls
deleteFile.txt helloword.cpp README.txt
toto@pc:~/code/testGit$ rm deleteFile.txt
toto@pc:~/code/testGit$ ls
helloword.cpp README.txt
toto@pc:~/code/testGit$ 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: deleteFile.txt
no changes added to commit (use "git add" and/or "git commit -a")
上面,我先是vim deleteFile.txt
了一个文件,然后将其add
和commit
进了仓库。
然后,直接使用自带的rm deleteFile.txt
将文件进行了删除,可以发现ls
后确实删除了。
用git status
可以看到Git提示:这个文件被删除。
但这仅仅时在文件目录管理中把它删除,我们的暂缓区中还有这个文件,使用命令git ls-files
可以看暂缓区有什么文件。
toto@pc:~/code/testGit$ git ls-files
README.txt
deleteFile.txt
helloword.cpp
这时,我们需要使用命令git rm deleteFile.txt
toto@pc:~/code/testGit$ git rm deleteFile.txt
rm 'deleteFile.txt'
toto@pc:~/code/testGit$ git ls-files
README.txt
helloword.cpp
看到没,命令git ls-files
后,我们的deleteFile.txt
文件已经从暂缓区移除出去了。
但之前因为使用了git commit
,所以这个文件应该还在master中
toto@pc:~/code/testGit$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: deleteFile.txt
所以,当我们在git rm
后,在git commit
一下:
toto@pc:~/code/testGit$ git commit -m "remove deleteFile.txt"
[master 05e641d] remove deleteFile.txt
1 file changed, 1 deletion(-)
delete mode 100644 deleteFile.txt
toto@pc:~/code/testGit$ git status
On branch master
nothing to commit, working tree clean
看到没,这样就删除掉了。 在这里一定要搞清暂缓区和master的区别
5.2 总结
- rm
- 把文件从文件目录删除 - git rm
-把文件从暂缓区删除 - git commit
- 如果该文件提交到了**master**中,记得在`git commit`下
补充:命令git ls-files
可以看暂缓区有什么文件。