Home  >  Article  >  Development Tools  >  What is the difference between the staging area and the local library in git?

What is the difference between the staging area and the local library in git?

青灯夜游
青灯夜游Original
2021-12-29 17:29:084736browse

The difference between the temporary storage area and the local library in git: The temporary storage area, also called the index area, is a transition area between the workspace and the local warehouse. It records the code status of the workspace (whether there are changes or not). , or what changes have been made); and the local repository records the status of the local code.

What is the difference between the staging area and the local library in git?

The operating environment of this tutorial: Windows 7 system, Git version 2.30.0, Dell G3 computer.

1. Understanding of concepts related to git warehouse

If you want to deeply understand git commands, you must understand several concepts related to git warehouse. First, take a look at the local code directory D:\gittest directory:

What is the difference between the staging area and the local library in git?
It should be noted that .git is a hidden directory and you need to display hidden files to see it.

Workspace (working directory): Simply put, files or directories saved locally belong to the workspace, and code modifications are usually performed in the workspace. Generally, local files, except for the hidden .git directory, belong to the workspace. The mydir, file1.txt, and file2.txt seen in the picture above all belong to the workspace.

Stage: Also known as the index area, as the name suggests, it is a transition between the workspace and the local warehouse. It records the code status of the workspace (Are there any changes, or what changes have been made). Located in the .git directory.

Local repository (repository) : Records the status of local code, located in the .git directory.

Understanding of Status:

Think about it, when we usually modify an important file, we must first make a backup in order to prevent modification errors. , and eventually recover. If the file itself is large, it will take up more space if backed up, which is not cost-effective. There is another way, which is to record the modification status of the file instead of backing up the file itself. The file status is similar to the following description:

The first line of the file: Replace xxx with ****

Between the third and fourth lines of the file, a line is added, the content is... …

To put it bluntly, the file status is the modification record of the file. Based on the modification record, we can know what modifications we have made to the file. According to the modification record, we can also restore the state before the file modification.

2. Explanation of git commands

Several commands related to viewing status:

git status View files at work Changes between areas, staging areas, and local warehouses

git diff View the differences between the workspace and the staging area

git diff --cached View the differences between the staging area and the local warehouse

git diff HEAD Check the differences between the workspace and the local warehouse

The corresponding operations of some commands are as follows:
What is the difference between the staging area and the local library in git?

The following is a specific example Explain that modification of local files will lead to changes in related areas

Before modification, the status between the workspace, staging area and local warehouse is consistent

(1) Locally modified file file1 Add a line in .txt add this line

git status Check the changes of files in the workspace, staging area, and local warehouse warehouse

What is the difference between the staging area and the local library in git?

As you can see in the picture above, Changes not staged for commit:, indicating that the file has changed in the workspace, but the modification has not yet been submitted to the temporary storage area

git diff View work Changes between the area and the staging area

从What is the difference between the staging area and the local library in git?

As can be seen from the figure, changes have occurred between the work area and the staging area. This is due to the modification of the local file, and The modifications have not been submitted to the staging area

git diff HEAD View the status between the workspace and the local warehouse

What is the difference between the staging area and the local library in git?

As can be seen from the above figure, Differences also arise between workspaces and local repositories. It's very simple. At this time, the status of the local warehouse and the staging area is consistent.

git diff --cached Check the staging area and the local warehouse and you will find that there is no difference between them.

(2) Submit local modifications to the temporary storage area

git add. Submit local modifications to the temporary storage area

git status

What is the difference between the staging area and the local library in git?
You can see that the modification has been submitted to the temporary storage area: Changes to be committed:

After submission, use git diff to check, you will find that the work area and the temporary storage area have been There is no difference anymore.

But there are differences between the staging area and the work area.

git diff --cached

What is the difference between the staging area and the local library in git?

Similarly, using git diff HEAD you will find that there are still differences between the workspace and the local warehouse

(3) Submit the changes to the local warehouse

git commit -m "add a line in file1.txt" Submit changes from the staging area to the local warehouse

Check the git status, prompting Your branch is ahead of 'origin/master' by 1 commit. Indicates that the changes in the workspace have been submitted to the local warehouse, but have not yet been pushed to the remote branch.

What is the difference between the staging area and the local library in git?

git diff, git diff --cached, git diff HEAD will find no difference in prompts. Because the modifications to the workspace have been submitted to the local warehouse, the status of the workspace, staging area, and local warehouse are consistent at this time.

Reverse operation

git add – git checkout returns the workspace file to the staging area state and replaces the workspace file with the staging area file.

For example, the workspace has modified the file file1.txt, but it has not been submitted to the temporary storage area at this time. If you want to give up the modification, you can use

git checkout file1.txt to return the file to the temporary storage area (abandon the modification)

git commit – git reset HEAD Pull the latest commit to the local The files in the warehouse are moved to the temporary storage area without affecting the work area.

Summary:

If we want to abandon a certain modification of the local file (workspace):

  • If the modification has not been submitted to Staging area, you can use git checkcout to restore
  • If the modification has been submitted to the staging area but has not been submitted to the local warehouse, first use git reset HEAD to restore the status of the staging area, and then use git checkout to restore Workspace

Recommended study: "Git Tutorial"

The above is the detailed content of What is the difference between the staging area and the local library 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