Git使用——将文件扔进垃圾桶

本文讲解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用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值