Git 删除文件

Git 删除文件

1.在Git中,删除也是一个修改操作,我们实战一下,先添加一个新文件test.txt到Git并且提交:

$ git add test.txt

$ git commit -m "add test.txt"
[master b84166e] add test.txt
 1 file changed, 1 insertion(+)
 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")

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

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

$ git commit -m "remove test.txt"
[master d46f35e] remove test.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 test.txt

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

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

$ git checkout -- test.txt

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

本文参考:
https://www.liaoxuefeng.com/wiki/896043488029600/900002180232448

2. 我本地操作git 删除文件的另外一个实例。

1 .在我本地手动删除文件之后,查看文件的去状态:

lollicup@DESKTOP-4M99Q31 MINGW64 /d/www/lollicupStore2 (sun_master_delcreateorder)
$ git status
On branch sun_master_delcreateorder
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:    app/code/local/Lollicupstore/Createorder/Model/Observer.php
        deleted:    app/code/local/Lollicupstore/Createorder/etc/config.xml
        deleted:    app/etc/modules/Lollicupstore_Createorder.xml
        modified:   media/customer/S/E/SELLERS_PERMIT.pdf
        modified:   media/customer/S/E/SELLER_PERMIT.pdf
        modified:   media/customer/S/e/Seller_Permit.pdf
        modified:   media/customer/S/e/Seller_permit.pdf

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

2.使用 git rm 删除

lollicup@DESKTOP-4M99Q31 MINGW64 /d/www/lollicupStore2 (sun_master_delcreateorder)
$ git rm app/code/local/Lollicupstore/Createorder/Model/Observer.php
rm 'app/code/local/Lollicupstore/Createorder/Model/Observer.php'

lollicup@DESKTOP-4M99Q31 MINGW64 /d/www/lollicupStore2 (sun_master_delcreateorder)
$ git rm app/code/local/Lollicupstore/Createorder/etc/config.xml
rm 'app/code/local/Lollicupstore/Createorder/etc/config.xml'

lollicup@DESKTOP-4M99Q31 MINGW64 /d/www/lollicupStore2 (sun_master_delcreateorder)
$ git rm app/etc/modules/Lollicupstore_Createorder.xml
rm 'app/etc/modules/Lollicupstore_Createorder.xml'

3. 查看其状态

$ git status
On branch sun_master_delcreateorder
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    app/code/local/Lollicupstore/Createorder/Model/Observer.php
        deleted:    app/code/local/Lollicupstore/Createorder/etc/config.xml
        deleted:    app/etc/modules/Lollicupstore_Createorder.xml

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

4. 使用 git commit -m “remove createorder module” 写提交注释

$ git commit -m "remove createorder module"
[sun_master_delcreateorder b4cb58a58] remove createorder module
 3 files changed, 128 deletions(-)
 delete mode 100644 app/code/local/Lollicupstore/Createorder/Model/Observer.php
 delete mode 100644 app/code/local/Lollicupstore/Createorder/etc/config.xml
 delete mode 100644 app/etc/modules/Lollicupstore_Createorder.xml

5.查看其提交之后的状态

$ git status
On branch sun_master_delcreateorder
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
no changes added to commit (use "git add" and/or "git commit -a")

6. 做一个$ git push操作

$ git push

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注