The principle and use of Git

1. Brief introduction

Before the emergence of Git, most companies still used SVN for project management, here is a comparison;

Centralized (SVN):

The centralized version control system has a single server with several types of management, which saves the revisions of all files, and people who work together connect to this server through the client to take out the latest files or submit updates;

Advantages: 1. Convenient management, in line with general logic; 2. High security; 3. High code consistency; 4. Suitable for development with a small number of people;

Disadvantages: 1. The pressure on the server is high, and the capacity of the database increases sharply; if you cannot connect to the server, it basically cannot work; 3. It is not suitable for open source development;

Distributed (Git):

In the distributed version control system, the client not only extracts the latest version of the file snapshot, but mirrors the original code warehouse completely. Therefore, if any server crashes, it can be recovered with any local warehouse; this system You can specify to interact with different remote code warehouses;

Advantages: 1. Suitable for distributed development, emphasizing individuals; 2. Public server pressure and data volume will not be too large; 3. Fast speed and high flexibility; 4. Any two developers are more likely to conflict; 5. Offline work ;

Disadvantage: poor code confidentiality, once the developer clones the entire library, all versions and version information can be disclosed;

2. Configuration information

配置用户名:git config --global user.name “xxx”
配置邮箱:git config --global user.email “xxx"
配置大小写敏感:git config --global core.ignorecase false
查看配置信息:git config --list

3. Basic principles

Git workspace:

1. Remote: remote warehouse, server hosting code (similar to github)

2. Repository: Warehouse area, that is, the local warehouse (.git folder), where data is safely stored, where all versions of data are submitted, and HEAD points to the latest version put into the warehouse;

3. index/Stage: Temporary storage area, used to temporarily store changes, is actually a file, used to save information that will be submitted to the file list;

4. workspace: workspace, that is, the location of editing files;

insert image description here

Understand the process of submitting code according to the above figure:

1. git add: Submit the files in the workspace to the temporary storage area;

2. git commit: Submit the files in the temporary storage area to the local warehouse;

3. git push: Submit the local warehouse to the remote warehouse;

Git file status:

  • Untracked : Untracked, in this folder, but not added to the git library, does not participate in version control, and becomes staged through git add status;
  • Unmodified : The file has been put into the warehouse and has not been modified, that is, the file snapshot content in the repository is consistent with the folder. There are two types of extraction of this type of file. If it is modified, it becomes modified. If it is removed, the repository git rm becomes is Untracked;
  • Modified : The file has been modified. This file has two destinations, the first is staged, and the second is unmodified;
  • Staged : Executing git commit will synchronize the modification to the library. At this time, although the files in the library are consistent with the local files, the files are Unmodified. Execute git reset HEAD filename to cancel temporary storage, and the file status is modified.

insert image description here

4. Basic operation

insert image description here

Here are some simple operations:

  • Initialize the local warehouse

    git init 	# 将本地文件夹变成一个本地仓库
    ll -ah		# 可以看到.git隐藏文件夹
    
  • add a new file

    git add text.txt		# 新增一个文件
    git commit -m "add a text.txt"	# 提交信息
    
  • If there is a bug after submitting the new version, roll back the version

    git log			# 查看提交的日志
    git reset --head Head^		# 回退到上个版本,^代表回退几个版本(如果已经commit,需要这样回退
    
  • Branch and tag management

    git checkout -b dev 	# 创建dev分支
    git branch				# 查看当前分支
    git checkout master		# 切换到master分支
    git merge dev			# 将dev分支合并到当前分支
    git branch -d dev		# 删除dev分支
    

V. Summary

Git actually does not have many operating commands, the main thing is to understand the concept of workspace and file status, which is the most essential part of Git;

When we use Git, we are not afraid of making mistakes. In fact, there will be a lot of room for error tolerance, and we are not afraid of someone suddenly deleting the library and running away haha;

Of course, Git commands are far more than those mentioned above, and there are many more that need to be continuously understood in actual use;

Guess you like

Origin blog.csdn.net/weixin_40620310/article/details/124265291