删除文件

新建一个新文件 test.txt 到 Git 并且提交:

$ git add test.txt
$ git commit -m "add test.txt"
[master de5e5e0] add test.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt

rm 命令删除 test.txt:

$ rm test.txt

再运行 git status 命令查看:

$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        deleted:    test.txt

no changes added to commit (use "git add" and/or "git commit -a")

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

$ git rm test.txt
rm 'test.txt'

$ git commit -m "remove test.txt"
[master 38c6f72] remove test.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 test.txt

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

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

$ git checkout -- test.txt

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

你可能感兴趣的:(删除文件)