Home >Development Tools >git >How to add and delete files in Git

How to add and delete files in Git

PHPz
PHPzOriginal
2023-04-03 09:17:071421browse

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

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

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"

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

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"
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!

Statement:
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