删除⽂件之git
删除⽂件
在 Git 中,删除也是⼀个修改操作,我们实战⼀下, 如果要删除 file5 ⽂件,怎么搞呢?如果你这样做了:
此时,⼯作区和版本库就不⼀致了,要删⽂件,⽬前除了要删⼯作区的⽂件,还要清除版本库的⽂件。
root@hcss-ecs-a74f:~# cd gitcode
root@hcss-ecs-a74f:~/gitcode# ls
file1 file2 file3 file4 file5 ReadMe
root@hcss-ecs-a74f:~/gitcode# rm file5
root@hcss-ecs-a74f:~/gitcode# ls
file1 file2 file3 file4 ReadMe
root@hcss-ecs-a74f:~/gitcode# git add file5
root@hcss-ecs-a74f:~/gitcode# git status
On branch master
Changes to be committed:(use "git restore --staged <file>..." to unstage)deleted: file5
⼀般⾛到这⾥,有两种可能:
• 确实要从版本库中删除该⽂件
• 不⼩⼼删错了
对第⼆种情况,很明显误删,需要使⽤ git 来进⾏恢复,很简单,我们刚学过(删除也是修改):
hyb@139-159-150-152:~/gitcode$ git checkout -- file5
hyb@139-159-150-152:~/gitcode$ ls
file1 file2 file3 file4 file5 ReadMe
对于第⼀种情况,很明显是没有删完,我们只删除了⼯作区的⽂件。这时就需要使⽤ git rm 将⽂件从暂存区和⼯作区中删除,并且 commit :
hyb@139-159-150-152:~/gitcode$ git rm file5
rm 'file5'
hyb@139-159-150-152:~/gitcode$ git status
On branch master
Changes to be committed:(use "git restore --staged <file>..." to unstage)deleted: file5
hyb@139-159-150-152:~/gitcode$ git commit -m"deleted file5"
[master 5476bde] deleted file51 file changed, 0 insertions(+), 0 deletions(-)delete mode 100644 file5
hyb@139-159-150-152:~/gitcode$ git status
On branch master
nothing to commit, working tree clean
现在,⽂件就从版本库中被删除了.
完整过程如下:
root@hcss-ecs-a74f:~# cd gitcode
root@hcss-ecs-a74f:~/gitcode# ls
file1 file2 file3 file4 file5 ReadMe
root@hcss-ecs-a74f:~/gitcode# rm file5
root@hcss-ecs-a74f:~/gitcode# ls
file1 file2 file3 file4 ReadMe
root@hcss-ecs-a74f:~/gitcode# git add file5
root@hcss-ecs-a74f:~/gitcode# git status
On branch master
Changes to be committed:(use "git restore --staged <file>..." to unstage)deleted: file5root@hcss-ecs-a74f:~/gitcode# git commit -m "delete file5"
[master 33b3d8d] delete file51 file changed, 0 insertions(+), 0 deletions(-)delete mode 100644 file5
root@hcss-ecs-a74f:~/gitcode# git status
On branch master
nothing to commit, working tree clean
root@hcss-ecs-a74f:~/gitcode# git rm file4
rm 'file4'
root@hcss-ecs-a74f:~/gitcode# git status
On branch master
Changes to be committed:(use "git restore --staged <file>..." to unstage)deleted: file4root@hcss-ecs-a74f:~/gitcode# git commit -m "delete file4"
[master 94cdba6] delete file41 file changed, 0 insertions(+), 0 deletions(-)delete mode 100644 file4