Git学习总结四(删除)

一般情况下,你通常直接在文件管理器中把没用的文件删了,或者用rm命令删了:

1 $ rm test.txt

这个时候,Git知道你删除了文件,因此,工作区和版本库就不一致了,git status命令会立刻告诉你哪些文件被删除了:

1 $ git status
2 On branch master
3 Changes not staged for commit:
4   (use "git add/rm <file>..." to update what will be committed)
5   (use "git checkout -- <file>..." to discard changes in working directory)
6 
7     deleted:    test.txt
8 
9 no changes added to commit (use "git add" and/or "git commit -a")

现在你有两个选择,一是确实要从版本库中删除该文件,那就用命令git rm删掉,并且git commit

1 $ git rm test.txt
2 rm 'test.txt'
3 
4 $ git commit -m "remove test.txt"
5 [master d46f35e] remove test.txt
6  1 file changed, 1 deletion(-)
7  delete mode 100644 test.txt

现在,文件就从版本库中被删除了。


另一种情况是删错了,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本:

1 $ git checkout -- test.txt

git checkout其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。

猜你喜欢

转载自www.cnblogs.com/ustc-anmin/p/10488787.html