如何从工作区、暂存区和Git仓库删除文件

版权声明:转载请注明出处。 https://blog.csdn.net/bagpiping/article/details/83827449

初始化场景:

  • 小明的工作区有文件 hello.txt
  • git add hello.txt
  • git commit -m "我把文件存到Git仓库了啊"

1、场景一:小明“不小心”把工作区的hello.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 checkout -- <file>..." to discard changes in working directory)

    deleted:   hello.txt

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

Git发现工作区和仓库版本不一样。
怎么办?
事实上Git已经提醒了,rm或者checkout

1.1 小明没有不小心,他确实要把文件删除
先删除 git rm hello.txt
再提交 git commit -m "i wanna remove test.txt"

1.2 小明确实不小心,他想买“后悔药”,Git帮可以他
一句代码还原精灵 git checkout --hello.txt
这么做有可能造成损失,因为还原的是Git仓库的,万一小明把最最新版的文件删除了那Git也帮不了他。小明只能去“垃圾回收站”看看了。

2、场景二:小红想把Git仓库和暂存区的文件删除,但是保留工作区的文件,怎么办?

Git帮她 git rm --cached hello.txt
文件还在磁盘上,就是不知道是不是小红需要的:0

猜你喜欢

转载自blog.csdn.net/bagpiping/article/details/83827449