web123456

git study notes-5 Delete files

existGitIn this article, deletion is also a modification operation. Let’s take a practical action. First add a new file to Git and submit:

  1. $ git add test.txt
  2. $ git commit -m "add "[master 94cdc44add 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 usermThe 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 statusThe command will tell you which files have been deleted immediately:

$ git statusOn 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 rmDelete it, andgit commit

  1. $ git rm test.txt
  2. rm ''
  3. $ 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 checkoutIn 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 rmUsed 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