existGitIn this article, deletion is also a modification operation. Let’s take a practical action. First add a new file to Git and submit:
-
$ git add test.txt
-
$ git commit -m "add "[master 94cdc44] add test.txt 1 file changed, 1 insertion(+) create mode 100644 test.txt
Generally speaking, you usually delete useless files directly in the file manager, or userm
The command has been deleted:
$ rm test.txt
At this time, Git knows that you have deleted the file, so the workspace and the repository are inconsistent.git status
The command will tell you which files have been deleted immediately:
$ 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 choices. One is to really delete the file from the version library. Then use the commandgit rm
Delete it, andgit commit
:
-
$ git rm test.txt
-
rm ''
-
$ git commit -m "remove "[master d17efd8] remove test.txt 1 file changed, 1 deletion(-) delete mode 100644 test.txt
Now, the file is deleted from the repository.
Another situation is that the delete is wrong, because there is still a version library, so it is easy to restore the accidentally deleted files to the latest version:
$ git checkout --
git checkout
In fact, it is to replace the version of the workspace with the version in the version library. No matter whether the workspace is modified or deleted, it can be restored with one click.
summary
Ordergit rm
Used to delete a file. If a file has been submitted to the version library, you will never have to worry about deleting it by mistake, but be careful, you can only restore the file to the latest version, and you will lose itWhat you modified after the last submission。