Home > Development Tools > git > Let's talk about how to delete and add files on git

Let's talk about how to delete and add files on git

PHPz
Release: 2023-04-03 10:12:15
Original
1787 people have browsed it

When using git to manage projects, we need to continuously add files and folders to the repository for version control and collaborative development. However, sometimes we may add wrong files or unnecessary files, and at this time we need to delete them from the repository. This article will introduce in detail how to delete added files in git.

  1. Check the git status

Before deleting the file, we need to check the git status to determine whether the file to be deleted has been submitted to the repository. You can use the following command to view the git status:

$ git status
Copy after login

If the git status contains files that need to be deleted, you can continue with subsequent deletion operations.

  1. Delete a single file

If you need to delete a single file, you can use the following command:

$ git rm [file]
Copy after login

where [file] represents the name of the file that needs to be deleted. After executing this command, git will delete the file from the repository and add the deletion operation to the staging area. At this time, you need to use the following command to submit the modification to the repository:

$ git commit -m "delete file"
Copy after login

Note that if you need to undo the deletion operation, you can use the following command:

$ git restore [file]
Copy after login

where [file] represents the file that needs to be restored name.

  1. Delete folder

If you need to delete the entire folder, you can use the following command:

$ git rm -r [folder]
Copy after login

where [folder] represents the folder that needs to be deleted name. After executing this command, git will delete the folder and all the files it contains from the repository, and add the deletion operation to the staging area. You also need to use the following command to submit the modification to the repository:

$ git commit -m "delete folder"
Copy after login

If you need to undo the deletion of the folder, you can use the following command:

$ git restore [folder]
Copy after login

where [folder] represents the name of the folder that needs to be restored. .

  1. Recover deleted files

If you need to recover deleted files, you can use the following command:

$ git checkout [commitID] -- [file]
Copy after login

where [commitID] indicates the need to restore The commit ID, [file] indicates the file that needs to be restored. After executing this command, git will restore the file to the specified commit ID version.

  1. Deletion error handling

In the process of using git to delete files, in some cases, files or folders that should not be deleted may be deleted, causing unnecessary Loss. At this time, we can handle it through the following methods:

  • Re-add the accidentally deleted files or folders
  • Restore the deleted commit ID version

If a submitted file is accidentally deleted, you need to directly restore the submitted commit ID version. If the accidentally deleted file is not submitted to the repository, it can be remedied by re-adding it.

  1. Summary

Git is one of the most popular version control tools at present. Through git, you can easily add files and folders to the repository for version control and Collaborative development. However, when using git, you also need to pay attention to the accuracy of deletion operations to avoid accidental deletions and data loss. This article introduces how to delete added files in git. I hope it can help you better use git for development and management.

The above is the detailed content of Let's talk about how to delete and add files on git. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template