在Git中,删除也是一个修改操作,我们实战一下,先添加一个新文件test.txt到Git并且提交:
$ git add test.txt
$ git commit -m "add test"
[master 55aa539] add test
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
一般情况下,你通常直接在文件管理器中把没用的文件删了,或者用rm命令删了:
$ rm 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")
文件目录中确实少了test.txt文件:
$ ls -a
./ ../ .git/ readme.txt
现在你有两个选择:
1.确实要从版本库中删除该文件,那就用命令git rm
删掉,并且git commit
$ git rm test.txt
rm 'test.txt'
$ git commit -m "remove test"
[master fbc228e] remove test
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 test.txt
$ git status
On branch master
nothing to commit, working tree clean
$ ls -a
./ ../ .git/ readme.txt
- 误删,需要将工作区文件恢复至版本库中最新版本。
直接使用git checkout
$ git checkout test.txt
$ git status
On branch master
nothing to commit, working tree clean
$ ls -a
./ ../ .git/ readme.txt test.txt
网友评论