Basic usage of git

This blogger just contacted git some time ago. Since git has not been used before, and as an ideal rookie in the front-end session, he should master the basic use of git, so he spent some time in private to learn the basic use of git. In the process of learning, he also summarized some opinions and experiences, so I will share them here.
Next, we share git in four aspects:

  1. The concept of git;

  2. Initialization of git warehouse;

  3. The working principle of git;

  4. Command commonly used by git;

1. The concept of git:
Before we talk about git, let's talk about another version control system, svn.
svn: centralized version controller, it has a central server, the complete version of the project is placed on the central server, each developer downloads the project from the central server, then develops, and finally uploads it to the central server.

The definition of git: Git is a distributed version control system, which is suitable for the application scenario where many people develop the same project at the same time.
Features: No central server, no networking, each developer has a complete project version, multi-person assistance, and multi-branch development, and then merge.

2. Initialization of GIT warehouse
Warehouse: version repository, project directory, all files in the warehouse are managed by git, including modification, deletion, recovery, etc.
Suppose a git repository is created in the workplace of the local F disk:
Step 1: Switch to workplace on disk F;

 cd F:/
 cd workplace/
 mkdir testgit
 cd testgit/
 pwd //Used to view the current path to determine whether the path is correct

When a directory is created, files with various suffix names can be created in that directory.
Finally, create a git repository for the directory:

git init

The files in the directory are then added to the git repository (described later).

3. The working principle of GIT
When using git, you should know several professional terms: workspace, cache, version library;
Workspace: Except for git warehouse in project directory (git warehouse usually exists as hidden file);
Cache: A stage area in the GIT warehouse, which is used to store temporary files (that is, files added by git add). The stage area is in the index folder in the GIT warehouse.
Version libraries: git repositories contain files that are submitted in the stage area.

Firstly, a file in the workspace adds new content or modifies some content. After adding or modifying, the file can be added to the stage area through git add.
Secondly, in the stage area, the contents added or modified in the file will be processed.

  1. Put new or modified content into the Object area, which creates an object to store the content.

  2. The stage area gets the corresponding objectId in the Object area and saves it (objectId is equivalent to an index).

Finally, the stage area is submitted to the local version library through git commit, at which time the master will get the objectId and find the corresponding content in the Object area according to the objectId.

4. Common commands for Git

1. Document processing:

git add filename //Add files to stage area
git commit -m "Notes" //Submit git add files into stage area to version Library
git  status  //Check if the file in the current stage area is commit
git diff filename //View Modified File Contents

2. Cancel the cache (that is, the file is still in the stage area, there is no commit)

git checkout --file //To undo the modification of the file in the workspace area, you also need to undo the git add to the stage area.
git reset Head --file //Unmodify the stage area file and restore it to the previous version
git rm -cached file //Delete files in stage area
git rm file //Delete files in workspace and stage areas

3. Version processing:

git log //View historical versions
git reflog //View version number
git reset --hard Specified version number //Returns the version to the specified version number
git reset --hard Head~N //Specifies the version number to be returned to the previous N

4. Branch Processing

git branch //View the current branch
git branch branchName //Create branch
git checkout branchName //Switch to a specified branch
git checkout -b branchName //Create and switch to a specified branch
git checkout -d branchName //Delete branch
git merge branchName //Merge branches with master branches

5. Remote library operation

git clone url //Clone a project version of the remote warehouse locally and generate a directory with the same name as the project name (for the first time)
git fetch //Extracting remote warehouse data and updating local data, but after updating, it also needs to be merged into the current branch of the developer.
git pull //Extract remote warehouse data to update local data and try to merge it into the current branch of the developer
git push [remoteName] [branchName] //Push local branch to remote warehouse branch with the same name
git remote //View Remote Warehouse
git remote -v //View remote warehouses and URLs
git remote add name url //Push the local warehouse in the name of name to the remote warehouse of the specified url
git remote rm  name //Delete remote warehouse

6. Code temporary storage

git stash //Push uncommitted content from the current workspace to the git stack, and the workspace will return to its original form, which can be submitted multiple times
git stash list //View the version number of all git stash on the GIT stack
git stash apply //Restore the last version number in the git stack to the workspace
git stash apply stash@{n} //Restore a version number from the specified git stack to the workspace
git stash pop //The syntax is the same as apply, except that apply will not delete the restored version from the git stack after recovery, while pop will
git stash clean //Clear git stack

This is the blogger's initial understanding of git. The use of Git is much more than these, so we need to supplement the knowledge of git in future project development.

Tags: Programming git svn Session

Posted on Mon, 27 May 2019 14:41:07 -0700 by SithLordKyle