YouTip LogoYouTip

Git Workspace Index Repo

* * * ## Basic Concepts Let's first understand the concepts of Git workspace, staging area and repository: * **Workspace:** It is the directory you can see on your computer. * **Staging Area:** English is called stage or index. Generally stored in the index file under the .git directory (.git/index), so we sometimes call the staging area index. * **Repository:** There is a hidden directory .git in the workspace, which is not part of the workspace, but Git's repository. The following diagram shows the relationship between the workspace, staging area in the repository, and the repository: !(#) * In the diagram, the left side is the workspace, and the right side is the repository. The area marked as "index" in the repository is the staging area (stage/index), and the one marked as "master" is the directory tree represented by the master branch. * As you can see in the diagram, "HEAD" is actually a "cursor" pointing to the master branch. Therefore, in the commands shown in the diagram, master can be used to replace HEAD. * The objects area in the diagram is Git's object library, which is actually located in the ".git/objects" directory and contains various created objects and their contents. * When you execute the git add command on files modified (or added) in the workspace, the directory tree in the staging area is updated, and the content of the modified (or new) files in the workspace is written to a new object in the object library, and that object's ID is recorded in the staging area's file index. * When executing a commit operation (git commit), the staging area's directory tree is written to the repository (object library), and the master branch is updated accordingly. That is, the directory tree pointed to by master is the staging area's directory tree at the time of commit. * When executing the git reset HEAD command, the staging area's directory tree is rewritten, replaced by the directory tree pointed to by the master branch, but the workspace is not affected. * When executing the git rm --cached command, the file is directly deleted from the staging area, and the workspace is not changed. * When executing the git checkout . or git checkout -- command, the files in the staging area (all or specified) are used to replace the files in the workspace. This operation is dangerous as it will clear changes in the workspace that have not been added to the staging area. * When executing the git checkout HEAD . or git checkout HEAD command, the files in the master branch pointed to by HEAD (all or partial) are used to replace the files in the staging area and the workspace. This command is also extremely dangerous, as it will not only clear uncommitted changes in the workspace but also clear uncommitted changes in the staging area. ### 1. Workspace (Working Directory) The workspace is your project directory on the local computer, where you perform file creation, modification, and deletion operations. The workspace contains all files and subdirectories of the current project. **Characteristics:** * Displays the current state of the project. * File modifications are made in the workspace, but these modifications have not yet been recorded in version control. ### 2. Staging Area The staging area is a temporary storage area that contains file snapshots that are about to be committed to the repository. Before committing, you can selectively add workspace modifications to the staging area. **Characteristics:** * The staging area saves changes that will be included in the next commit. * You can use the `git add` command multiple times to add files to the staging area until you are ready to commit all changes. **Common Commands:** git add filename # Add a single file to the staging area git add . # Add all modifications in the workspace to the staging area git status # View which files are in the staging area ### 3. Repository The repository contains all version history records of the project. Each commit creates a new snapshot in the repository, and these snapshots are immutable, ensuring the complete historical record of the project. **Characteristics:** * The repository is divided into local repository and remote repository. Here we mainly refer to the local repository. * The local repository is stored in the `.git` directory, which contains all committed objects and references. **Common Commands:** git commit -m "Commit message" # Commit the changes in the staging area to the local repository git log # View commit history git diff # View the differences between the workspace and the staging area git diff --cached # View the differences between the staging area and the last commit ### Relationship Between Workspace, Staging Area and Repository **1. Workspace -> Staging Area** Use the git add command to add workspace modifications to the staging area. git add filename **2. Staging Area -> Repository** Use the git commit command to commit the modifications in the
← Java ExamplesMysql Install Setup β†’