Git删除文件

使用场景

当要在工作区删除一个文件并提交到版本库中时,就需要用到git rm命令了。不同于git add添加或修改一个文件,git rm是删除一个文件,并结合git commit将删除提交到版本库中。

 

案例演示

改动之前工作区

➜  Code git:(master) ll
total 8
drwxr-xr-x   5 mymac  staff   160  9  7 08:00 ./
drwxr-xr-x+ 64 mymac  staff  2048  9  7 08:36 ../
drwxr-xr-x  13 mymac  staff   416  9  7 08:36 .git/
drwxr-xr-x   3 mymac  staff    96  8  3 15:29 cliff_demo/
-rw-r--r--   1 mymac  staff    21  8 25 18:12 master1.txt

现在创建一个名为 __init__.py 的文件,并将它提交到版本库中。

➜  Code git:(master) touch __init__.py
➜  Code git:(master) ✗ ll
total 8
drwxr-xr-x   6 mymac  staff   192  9  7 08:38 ./
drwxr-xr-x+ 64 mymac  staff  2048  9  7 08:38 ../
drwxr-xr-x  13 mymac  staff   416  9  7 08:38 .git/
-rw-r--r--   1 mymac  staff     0  9  7 08:38 __init__.py
drwxr-xr-x   3 mymac  staff    96  8  3 15:29 cliff_demo/
-rw-r--r--   1 mymac  staff    21  8 25 18:12 master1.txt
➜  Code git:(master) ✗ git add __init__.py
➜  Code git:(master) ✗ git commit -m "demo create a python file."
[master b231ce6] demo create a python file.
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 __init__.py
➜  Code git:(master) ll
total 8
drwxr-xr-x   6 mymac  staff   192  9  7 08:38 ./
drwxr-xr-x+ 64 mymac  staff  2048  9  7 08:38 ../
drwxr-xr-x  13 mymac  staff   416  9  7 08:38 .git/
-rw-r--r--   1 mymac  staff     0  9  7 08:38 __init__.py
drwxr-xr-x   3 mymac  staff    96  8  3 15:29 cliff_demo/
-rw-r--r--   1 mymac  staff    21  8 25 18:12 master1.txt

使用 git rm 将版本库中的 __init__.py文件删除

使用git rm 命令后,工作区对应的文件会被删除。

➜  Code git:(master) git rm __init__.py
rm '__init__.py'
➜  Code git:(master) ✗ ll
total 8
drwxr-xr-x   5 mymac  staff   160  9  7 08:41 ./
drwxr-xr-x+ 64 mymac  staff  2048  9  7 08:41 ../
drwxr-xr-x  13 mymac  staff   416  9  7 08:41 .git/
drwxr-xr-x   3 mymac  staff    96  8  3 15:29 cliff_demo/
-rw-r--r--   1 mymac  staff    21  8 25 18:12 master1.txt

 

这时版本库中的文件仍未删除,仅仅只是工作区删除了。我们需要继续使用 git commit 将删除修改提交到版本库中。

➜  Code git:(master) ✗ git commit -m "demo delete python file."
[master 45669b1] demo delete python file.
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 __init__.py
➜  Code git:(master)

 

至此删除版本库中的一个文件的任务就算完成了。

你可能感兴趣的:(Git)