本文讲解Git删除文件操作
新添加一个helloworld.txt文件并将其提交
# 添加文件
$ git add helloworld.txt
# 提交
$ git commit -m "helloworld"
[master 27e7eca] helloworld
1 file changed, 1 insertion(+)
create mode 100644 helloworld.txt
如果不想要了,如何将其删除呢?
# 在文件管理器中删除文件
$ rm helloworld.txt
这时,工作区和版本库就不一致了,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 restore <file>..." to discard changes in working directory)
deleted: helloworld.txt
有两个选择。
1.可以将版本库中的文件删掉
# 删除
$ git rm helloworld.txt
rm 'helloworld.txt'
# 提交删除
$ git commit -m "remove helloworld"
[master 11e35a9] remove helloworld
1 file changed, 1 deletion(-)
delete mode 100644 helloworld.txt
2.删错了,需要恢复文件的版本
# 恢复文件
$ git checkout -- test.txt
注意:命令git rm用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容。