Home > Development Tools > git > body text

How to add and delete files in Git

PHPz
Release: 2023-04-03 09:54:01
Original
1366 people have browsed it

Git is a very common and popular version control tool that makes it easy to track and manage project changes. In actual application processes, files are often added or deleted. Next, we will introduce in detail how to add and delete files in Git.

Add files

The way to add files is very simple, just use Git’s add command. Suppose we need to add a file named example.txt, then perform the following operations:

git add example.txt
Copy after login

After executing the above command, Git will add the file to the staging area. At this point, we can use the git status command to view the current status:

$ git status
On branch master

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   example.txt
Copy after login

You can see that Git has added the example.txt file to the list of upcoming submissions (That is, the temporary storage area).

If you want to commit all changes in the staging area at once, you can use the git commit command:

git commit -m "add example.txt file"
Copy after login

After executing the above command, Git will create a new Commit and apply the changes in the staging area to the commit.

Deleting files

Deleting files is also easy, just use Git's rm command. Suppose the file name we need to delete is example.txt, then perform the following operations:

git rm example.txt
Copy after login

This operation will delete example.txt## from the local file system # file and add a corresponding record in the staging area. If you want to delete this file from the repository, you need to submit it again:

git commit -m "delete example.txt file"
Copy after login
After executing the above command, Git will create a new submission and record that the file has been deleted.

It should be noted that if you re-add a file with the same name after submitting, Git will still treat it as a new file rather than a reconstructed file. This is because Git's version control is based on file contents rather than file names.

Summary

Adding and deleting files is one of the commonly used operations in Git. The operation is simple and easy to learn, but in practice you also need to pay attention to some details. I hope this article helps you understand the operations of adding and deleting files in Git.

The above is the detailed content of How to add and 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!