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:
- Record snapshots directly instead of diffs
- 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:
- After the network is disconnected, the version management of the project can still be performed locally
- 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
- 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.
- Staged: A file is staged if it has been modified and put into the staging area.
- 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:
- Modify files in the workspace
- Stage the changes you want to commit next time
- 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.name
and 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.email
C:/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)
- view in browser
git help config
- View in terminal:
git config -h
1. 在浏览器中查看 :git help config
2. 在终端中查看: git config -h
Basic operations of Git
Two ways to get Git repository
- Convert an unversioned local directory into a Git repository
- 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:
- In the project directory, open "Git Bash" by right mouse button
- 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
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)
- Remove the file in the git repository and the workspace at the same time
git rm -f 目标文件
- The value removes this file from the git repository, but keeps this file in the workspace
git 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:
# 开头
with is the comment/ 结尾
with is the directory- to
/ 开头
prevent recursion ! 开头
Negate with- 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
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.
Fall back to the specified version
Use the git reset --hard command to return to the specified version according to the specified ID
Summary: Common basic operations in GIT
- Command to initialize a Git repository
git init
- command to view file status
git status
orgit status -s
- A command to add files to the temporary storage area at one time
git add .
- 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~~~