Git operation [detailed] [detailed]

What is Git

Git is an open source distributed version control system, which is currently the most advanced and popular version control system in the world. Versioning of very small to very large projects can be handled quickly and efficiently.

Features: The bigger and more complex the project, the more collaborative developers, the more it can reflect the high performance and high availability of Git

Features of Git

The reason why Git is fast and efficient mainly depends on its following two characteristics:

  1. Record snapshots directly instead of diffs
  2. Nearly all operations are performed locally

Difference comparison with SVN

Traditional version control systems (such as SVN) are difference-based version control. They store a set of basic files and the differences of each file gradually accumulated over time.

Benefits: Save disk space
Disadvantages: Time-consuming and inefficient

Every time the version is switched, each difference needs to be applied on the basis of the basic file, so as to generate the file corresponding to the target version.

Git's record snapshot

Git snapshot is to regenerate a new file based on the original file version, similar to backup. For efficiency, if the file has not been modified, Git does not re-store the file, but only keeps a link pointing to the previously stored file.

Disadvantage: takes up a lot of disk space
. Advantage: version switching is very fast, because each version is a complete file snapshot, and the snapshot of the target version can be directly restored when switching versions.
Features: space for time

Nearly all operations are performed locally

Most operations in Git only require access to local files and resources, and generally do not require information from other computers on the network.

characteristic:

  1. After the network is disconnected, the version management of the project can still be performed locally
  2. After networking, just synchronize the locally modified records to the cloud server

Three areas in Git

Projects managed by Git have three areas, namely 工作区, 暂存区,Git 仓库

Three states in Git

  1. Modified: The file in the workspace has been modified, but it has not been placed in the temporary storage area, which is the modified state.
  2. Staged: A file is staged if it has been modified and put into the staging area.
  3. Submitted: If a specific version of the file is saved in the Git repository, it is submitted

Basic Git workflow

The basic Git workflow is as follows:

  1. Modify files in the workspace
  2. Stage the changes you want to commit next time
  3. Submit the update, find the file in the staging area, and permanently store the snapshot to the Git repository

Git configuration and installation

Download the installation package suitable for your computer from the Git official website ,

Configure user information

Configuration is done using the command line:

1. 配置用户名
git config --global user.name "在这里填写用户名"
2. 配置邮箱
git config --global user.email "邮箱"

tips: If the --global option is used, the command only needs to be run once to take effect permanently

Where to view Git's global configuration file

The username and email address configured by git config --global user.nameand will be written into the file. This file is Git's global configuration file, which can take effect permanently after being configured once.git config --global user.emailC:/Users/用户名文件夹/.gitconfig

Check configuration information

3. 查看所有全局配置项
git config --list --global
4. 查看指定全局配置项
git config user.name
git config user.email

Get help information (command in two ways)

  1. view in browsergit help config
  2. View in terminal:git config -h
1. 在浏览器中查看   :git help config
2. 在终端中查看: git config -h 

Basic operations of Git

Two ways to get Git repository

  1. Convert an unversioned local directory into a Git repository
  2. Clone an existing Git repository from another server

Both of the above methods can get an available Git repository on your computer

Initialize the repository in an existing directory

If you have a project directory that has not been under version control and want to use Git to control it, you need to perform the following two steps:

  1. In the project directory, open "Git Bash" by right mouse button
  2. Execute the git init command to convert the current directory into a Git warehouse

The git init command will create a hidden directory named .git. This .git directory is the Git repository of the current project, which contains the initial necessary files, which are a necessary part of the Git repository.

tips:If the .git file is not displayed, it does not mean that there is an error, we just do not display the hidden files by default on the computer, you can click on the top to view in my computer, and then select View hidden files;

4 states of files in git workspace

insert image description here

Git 操作的终极结果:Let the files in the workspace be in 未修改the state of " ", and then allow us to operate.

Check the status of the file

	git status(显示的非常全面)
	git status -s(仅仅显示文件的状态,非常精简)

Result analysis of query status:

	1. ?? 代表未被git管理;
	2. A代表现在是从原来未被管理的状态,变成了被管理的状态,并且已经进入了工作区。此时如果进行修改,那就会变成红色的M,并且没有进入暂存区,想进去再用git add
	3. 红色M代表我们被跟踪的文件被修改了,但是还没有放到暂存区中。
	4. 绿色的D代表的是这个文件已经彻底被移除git了

Add trace file

git add 文件名                          (eg: git add index.html)

Submit the files in the temporary storage area to the git warehouse

git commit -m "在这里写描述信息,也可以不写"    -m的作用就是添加描述信息

If you want to modify the file, 放到暂存区then we have git add +这个文件to .

The result of the query status after running is: the red M becomes a green M (indicating that the modified file has been placed in the temporary storage area)

Undo the modification of the specified file (the risk is relatively high, operate with caution) because there is no way to restore it after the undo

git checkout -- 文件名

Add multiple files at once to the staging area

git add .  

Remove files from staging area

git reset 	HEAD  +要移除的文件名称

Remove all files in the temporary storage area

git reset HEAD .

Directly submit the files in the workspace to the git warehouse (-a means to skip the temporary storage area, and -m means to add description information)

git commit -a -m "描述信息"

Remove the corresponding file from the git repository (there are two kinds)

  1. Remove the file in the git repository and the workspace at the same timegit rm -f 目标文件
  2. The value removes this file from the git repository, but keeps this file in the workspacegit rm -- cached 目标文件

Ignore files, so that files that do not need to be included in git management will not be displayed in the list that is not managed by git

Create a file named .gitignore on the root directory, and then write the file syntax that needs to be ignored in this file

The format specification of the file .gitignore:

  1. # 开头with is the comment
  2. / 结尾with is the directory
  3. to / 开头prevent recursion
  4. ! 开头Negate with
  5. You can use glob 模式to match files and folders (glob refers to simplified regular expressions)

glob pattern
6. 星号 *Match zero or more arbitrary characters
7. [abc]Match any one of the characters listed in square brackets (this case matches an a or matches a b or matches a c)
8. 问号 ?Matches only one arbitrary character
9. In the square Two characters are used to 短划线separate the brackets, which means that all within the range of these two characters can be matched (such as [0-9] means to match
all numbers from 0 to 9)
10. 两个星号 **means 匹配任意中间目录(such as a/**/z can match a/z, a/b/z or a/b/c/z, etc.)

eg: Example of a .gitignore file
insert image description here

View a project's commit history

If you want to review the commit history of your project, you can use git log, a simple and effective command.

git log
insert image description here

insert image description here
insert image description here

Fall back to the specified version

insert image description here
Use the git reset --hard command to return to the specified version according to the specified ID
insert image description here
insert image description here
insert image description here
Summary: Common basic operations in GIT

  1. Command to initialize a Git repository
    git init
  2. command to view file status
    git statusorgit status -s
  3. A command to add files to the temporary storage area at one time
    git add .
  4. The command to submit the files in the temporary storage area to the Git warehouse
    git commit -m "提交消息"

Keep reviewing, keep being excellent, be practical and do things seriously; pay attention to Sanlian and keep updating~~~

insert image description here

Guess you like

Origin blog.csdn.net/egg_er/article/details/122720682