Git installation, principle introduction and basic usage tutorial (super detailed)

1. Installation

Before using Git, you need to install Git.

Currently supports running on Linux/Unix, Solaris, Mac and Windows platforms.

Git installation package download address for each platform: Git - Downloads

Installation on Windows platform:

Installation package download address: Git for Windows

The official website is slow, you can use domestic mirror: CNPM Binaries Mirror

2. 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.
  • Temporary storage area: called stage or index in English. It is generally stored in the index file (.git/index) in the .git directory, so we sometimes call the temporary storage area the index.
  • Repository: There is a hidden directory .git in the workspace. This is not the workspace, but the Git repository.

The following figure shows the relationship between the workspace, the staging area in the repository, and the repository:

2. Basic operations

Git's job is to create and save snapshots of your project and compare them to subsequent snapshots.

This chapter introduces the commands for creating and submitting snapshots of your project.

The following 6 commands are commonly used in Git: git clone , git push , git add  , git commit , git checkout , and git pull . Let’s take a brief look at them.

illustrate:

  • workspace: workspace
  • staging area: staging area/cache area
  • local repository: version library or local warehouse
  • remote repository: remote warehouse

3. Common usage scenarios

1. Temporary storage-submit-pull-push

git add . (这里注意add与.之间是有空格的)

git commit -m '提交备注'

git pull

git push

2. Branch merge

Suppose I made changes in branch a and want to merge the code into branch b. How should I do this?

2.1. Execute temporary storage-submit-pull-push in branch a .

git add . (这里注意add与.之间是有空格的)

git commit -m '提交备注'

git pull

git push

2.2. Then switch to branch b and execute the merge command

git merge a

Then perform pull and push

git pull

git push

3. Rollback code

You need to use git reset . There are three different rollback methods.

3.1. Restore the previous version, retain the work area, and prepare the cache area to submit the commit again.

git resert --soft head^

Usage scenario : merge multiple commits into one

3.2. Restore the current version, retain the workspace, and clear the cache area.

git resert --mixed head

Usage scenario : I want to modify the wrong commit

3.3. Restore the current version and delete the modifications in the workspace and cache area. 

git reset --hard head 

You can also directly switch to the specified version number 6346e, and you can see the version number of each submission in the local git management.

git resert --hard 6346e 

Usage scenario : Discard all modifications after the target version

4. Acknowledgments

        Thank you all for your reading and support, and I wish you all a happy study!

Guess you like

Origin blog.csdn.net/Leewayah/article/details/131617422
Recommended