[Git] - workspace, staging area, version library, remote warehouse

1, Concept

1. Four working areas

Git has four local work areas: Working Directory, stage / index, Repository or git directory, and git remote directory. The conversion relationship between these four areas is as follows:

Workspace: workspace is where you usually store project code

Index / Stage: temporary storage area, which is used to temporarily store your changes. In fact, it is just a file to save the information to be submitted to the file list

Repository: the warehouse area (or version Library) is a safe place to store data, which contains the data you submit to all versions. Where HEAD refers to the latest version put into the warehouse

Remote: remote warehouse, a server hosting code, can be simply regarded as a computer in your project team for remote data exchange

2. Workflow

The workflow of git is generally as follows:

1. Add and modify files in the working directory;

2. Put the files requiring version management into the temporary storage area;

3. Submit the files in the staging area to the git warehouse.

Therefore, git manages files in three states: modified, staged, and committed

3. Four states of documents

Version control is the version control of a file. To modify or submit a file, you must first know the current status of the file. Otherwise, you may submit a file you don't want to submit, or the file you want to submit is not submitted.

GIT does not care about the specific difference between the two versions of the file, but whether the whole file has changed. If the file has been changed, a snapshot of the new version of the file will be generated when adding and submitting, and the method to judge whether the whole file has changed is to use

SHA-1 algorithm calculates the checksum of the file.

Untracked: not tracked. This file is in the folder, but it is not added to the GIT library and does not participate in version control Through git add, the status changes to Staged

Unmodify: the file has been put into storage and not Modified, that is, the contents of the file snapshot in the version library are exactly the same as those in the folder This type of file has two destinations. If it is Modified, it becomes Modified

If you use git rm to remove the version library, it becomes an Untracked file

Modified: the file has been modified. It is only modified without other operations There are also two places for this file. You can enter the staged state through git add, and discard the modified files using git checkout,

Return to the unmodified state, and this git checkout will take out the file from the library and overwrite the current modification

Staged: staging status Execute git commit to synchronize the changes to the library. At this time, the files in the library and the local files become consistent, and the files are in unmodified state Execute git reset HEAD filename to cancel the temporary storage,

The file status is Modified

The following figure well explains the transformation of these four states:

New file -- > untracked

Use the add command to add the newly created file to the staging area -- > staged

Use the commit command to submit the files in the staging area to the local warehouse -- > unmodified

If you modify a file in Unmodified status -- > modified

If you remove a file in Unmodified status -- > untracked


2, Common commands in four areas

1. Create a new code base

# Create a Git code base in the current directory
 git init
# Create a new directory and initialize it as a Git code base
git init [project-name]
# Download a project and its entire code history
git clone [url]

2. View file status

#View specified file status
git status [filename]
#View all file status
git status

3. Work area < -- > staging area

# Adds the specified file to the staging area
git add [file1] [file2] ...
# Adds the specified directory to the staging area, including subdirectories
git add [dir]
# Add all files in the current directory to the staging area
git add .
#When we need to delete the file on the staging area or branch, and the file is not needed in the workspace, we can use( ⚠️)
git rm file_path
#When we need to delete the files in the temporary storage area or branch, but need to use them locally, there is no file directly pushed at this time. If we add again before pushing, there will still be.
git rm --cached file_path
#Directly add the file name to restore the file from the staging area to the workspace. If the file already exists in the workspace, overwrite will be selected
#Adding [branch name] + file name means to pull the file from the branch name written by the branch name and overwrite the file in the workspace
git checkout

4. Workspace < -- > resource library (version Library)

#Put staging area -- > resource library (version Library)
git commit -m 'Description of this submission'
#If it occurs: commit unnecessary files or think it is wrong to submit them last time, or do not want to change the contents of the temporary storage area, but just want to adjust the submitted information
#Remove unnecessary files added to the staging area
git reset HEAD file name
#Remove the last submission (it will directly change to the state before add)   
git reset HEAD^ 
#Remove the last submission (after it becomes add, the status before commit) 
git reset --soft  HEAD^ 

5. Remote operation

# Retrieve the changes of the remote warehouse and merge with the local branch
git pull
# Upload specified local branch to remote warehouse
git push

6. Other common commands

# Displays the current Git configuration
git config --list
# Edit Git profile
git config -e [--global]
#Before the initial commit, you need to configure the user mailbox and user name, and use the following commands:
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
#Call up Git's help document
git --help
#View the help document for a specific command
git +command --help
#View git version
git --version


Source: https://www.cnblogs.com/best/p/7474442.html


Tags: git

Posted by magicmoose on Fri, 20 May 2022 22:03:50 +0300