Home > Development Tools > git > body text

Details on how to delete files in git

PHPz
Release: 2023-04-03 09:43:58
Original
7482 people have browsed it

When using git for version control, we may encounter situations where files need to be deleted. However, deleting files with git is not as simple as ordinary file deletion. Next, this article will introduce in detail how to delete files in git.

  1. Delete files from git

When we need to delete files in git, we can use the following command:

git rm file_name
Copy after login

This command will The file is deleted from the git staging area and workspace, and the deletion operation of the file is also added to the staging area and submitted to the local warehouse. If you want to delete the file from the local repository, you need to use the following command:

git commit -m "delete file_name"
Copy after login

This will submit a commit that includes the deletion of the file. The function of this command is to add the deletion of the file to the git version control history. When we need to synchronize this deletion operation to the cloud, we can use the following command:

git push
Copy after login
  1. Recover deleted files

Sometimes we may need to restore deleted files For deleted files, we can use the following command:

git checkout commit_id file_name
Copy after login

Among them, commit_id is the commit number of the file that needs to be restored before it is deleted, and file_name is the name of the file that needs to be restored. This command will restore the file from the specified commit and write it to the current working directory. If we want to add the recovery operation of the file to the local warehouse, we can use the following command:

git add file_name
git commit -m "restore file_name"
Copy after login

This will add the recovery operation of the file to the staging area and the local warehouse.

  1. Ignore deleted files

Sometimes we may not want to submit the deleted files to the local warehouse, but git will delete the files by default Submit to the local repository as a commit. In order to ignore these deleted files, we can use .gitignore file.

.gitignore file is a special file used to specify a list of files or directories that do not need to be submitted to git version control. Just add file names or wildcards that need to be ignored in the file. For example, if I want to ignore all log files and tmp directories, I can add the following content in the .gitignore file:

*.log
tmp/
Copy after login

In this way, when we delete a log file or /tmp directory, git will The deletion operation is automatically ignored and will not be submitted to the local warehouse.

Summary

Deleting files in git is not a simple matter. In addition to deleting the staging area and workspace of the file, the deletion operation also needs to be added to the local warehouse and submitted to the cloud. If you want to recover deleted files, you need to restore them based on the commit number. In addition, in order to ignore some deleted files that are unnecessary for submission, settings can be made in .gitignore. I hope this article can help you better understand deleting files in git.

The above is the detailed content of Details on how to delete files in 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