In Git, deletion is also a modification operation. Let's try it out. First add a new file test.txt to Git and submit it:
$ git add test.txt
$ git commit -m "add test.txt"
[master 94cdc44] add test.txt
1 file changed, 1 insertion(+)
create mode 100644 test.txt
Under normal circumstances, you usually delete useless files directly in the file manager, or rm
delete them with the command:
$ rm test.txt
At this point, Git knows that you deleted the file, so the workspace and the repository are inconsistent, and the git status
command will tell you immediately which files were deleted:
$ 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")
Now you have two options, one is if you really want to delete the file from the repository, then git rm
delete it with the command, and git commit
:
$ git rm test.txt
rm 'test.txt'
$ git commit -m "remove test.txt"
[master d17efd8] remove test.txt
1 file changed, 1 deletion(-)
delete mode 100644 test.txt
The file is now removed from the repository.
Another case is that it is deleted by mistake, because there is still one in the repository, so you can easily restore the mistakenly deleted file to the latest version:
$ git checkout -- test.txt
git checkout
In fact, it replaces the version of the workspace with the version in the repository. No matter whether the workspace is modified or deleted, it can be "one-click restore".
summary
command git rm
is used to delete a file. If a file has already been committed to the repository, then you never have to worry about deleting it by mistake, but be careful, you can only restore the file to the latest version, you will lose your changes since the last commit .