Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Git deletes files in a staging area or version library

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "Git how to delete files in the temporary storage area or version library". In the daily operation, I believe many people have doubts about how to delete files in the temporary storage area or version library in Git. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to delete files in temporary storage area or version library by Git". Next, please follow the editor to study!

Basics

Git has three major areas (workspace, temporary storage area, version library) and several states (untracked, unstaged, uncommited). The following is just a brief description of the general workflow of Git. For details, you can refer to other articles about Git.

Open your project folder, in addition to the hidden .git folder, other project files are located in the workspace, the workspace files need to be added to the Git temporary storage area (git add), and then submitted to the Git version library (git commit).

The files created for the first time are in untracked status (untracked). If you need to git add to the temporary storage area, Git will generate an index of the file in the temporary storage area. The file is in uncommited state at this time, and git commit is required to generate a version library. After adding to the version library, then modify the file, then the state of the file will change to the unstaged state.

With a simple understanding of the workflow of Git, you can see how to delete files that have been mistakenly added to the staging area or version library.

Delete files that were incorrectly added to the staging area

Sometimes when you create a new file TestFile in the workspace and have added it to the staging area, git will inform you that there is an existing file that has not been submitted to the version library, as shown in the following figure:

Not submitted to the version library only delete files in the staging area

At this point, if you want to undo the file that the error added to the staging area, you can enter the following command:

Git rm-- cache file name

Undo error added to staging area

The above command only deletes the files in the temporary storage area and will not affect the files in the workspace. As shown in the figure above, TestFile.txt still exists. Enter the following command at this time, and git will tell you that there is an untracked file TestFile.txt.

Git status

Delete the file git rm-f filename of the staging area and workspace

The files in the workspace have also been deleted.

Delete the wrong submitted commit from the file in the workspace

Sometimes, when not only the commit is added to the staging area, but also the git rm is added to the version library, you can't use git rm at this time, and you need to use the git reset command.

The error is submitted to the version library, and the contents of the three are all the same, regardless of the workspace, the temporary storage area, or the version library, so in this case, only the files in the workspace and temporary storage area are deleted. The next time you roll back the mistakenly added file with this version library, it will be regenerated.

At this time, we must cancel the revision of the version library in order to solve the problem!

Git reset has three options,-- hard,-- mixed,-- soft.

/ / only revoke the submitted version library and will not modify the temporary storage area and workspace

Git reset-- soft version library ID

/ / only revoke the submitted version library and temporary storage area, and will not modify the workspace

Git reset-- mixed version library ID

/ / completely restore the workspace, staging area, and version library records to the specified version library

Git reset-- hard version library ID

So which option should we use?

If you have made changes to the workspace code after you have submitted, and want to keep these changes, then you can use git reset-mixed version library ID, note that this version library ID should not be the version library you just submitted, but the last version library you just submitted. As shown below:

If you don't want to keep these changes, you can simply use the complete restore command, git reset-- hard version library ID.

Why not use-- soft, because it just restores the version library, the staging area still has the index of the files you mistakenly submitted, and you need to further use the deletion errors in the previous section to add files to the staging area, as detailed above.

At this point, the study on "how to delete files in the temporary storage area or version library by Git" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report