Git installation
Install
1. First go to the official website to download this software, and prepare to install it on this computer
git-scm.com/
2. Download this software to the machine according to your own computer system
The Windows system can directly download the .exe file, the macOS system uses the Homebrew command line to install, and the terminal enters git --version to confirm the installation
3. The default installation path can be selected by default. If you want to change the path, be sure to use the English path
4. For the Windows system, check whether the installation is successful: Right-click on any folder, check if there is a Git Base Here option, if there is, it is successful
introduce
Three areas of Git:
- workspace: the area where work is done
- Temporary storage area: an area for temporary storage
- Local git warehouse: final storage area
In the folder is reflected as follows:
- Workspace: the directory you see on your computer
- Temporary storage area: in the index in the .git folder (binary records)
- Repository: Refers to the entire .git folder (also considered a local warehouse)
It is reflected in the code as follows:
Git uses
Official Documentation: Tutorial Link
Rookie Tutorial: Tutorial link
Git configuration
After installing Git, the first thing to do is to set up your username and email address. Because every Git commit will use this information
The command format is as follows: Chinese depends on the situation
git config fixed command, set git related configuration
--global Global configuration: Once configured, the whole machine will take effect when using git
git config --global user.name 你的用户名
git config --global user.email 你的邮箱地址
The effect of running the command is as follows:
After configuration, you can run the following command to check whether it is successful
git config --list
#如果信息太多,可以输入 q 退出
The effect of running the command is as follows:
If the above content appears, the registration is successful
If you want to modify it later, you only need to execute the command again
Mark on the right side of the file in vscode
Generally, when opening a folder containing a git warehouse, there will be these marks
右侧没有标记的时候为“未修改” 或 此文件/文件夹, 被git忽略不跟踪变化
M为“已修改”
A为“已添加”
D为“已删除”
R为“已重命名”
C为“已复制”
U为“已更新但为融合”
Git basic commands
Initialize an empty Git repository
We create a new folder/existing folder is not a git warehouse, because there is no .git folder, so it is not managed by git. In the new folder/existing folder, you can run the following command to get the .git folder, and then let git start preparing for management
# Initialize the git warehouse, product: .git folder (managed "inside" the folder) git init
For example, enter the git init command in a new folder to initialize an empty git repository
After successfully initializing the empty git warehouse, in the project folder, display hidden files, and you can view the .git folder. For Windows systems, check the Hidden Items option in View. For macOS system, use the shortcut key Command + Shift + . to switch hidden file display
Record update to Git repository
Whenever you complete the goal of a stage and want to document it, commit it to the repository
Core operation: workspace development ---> add the modified file to the temporary storage area ---> record the files in the temporary storage area to the repository
-
To change the workspace and put it in the temporary storage area, the following command
bashcopy code
# (了解) 将index.html 添加到暂存区 git add index.html # 将css目录下一切添加到暂存区 git add css
-
The following command can put all the changed files into the temporary storage area at one time
Note: There is a space after add, which
.
means that all changes in the current directory are temporarily storedcopy code
git add .
-
Submit the content of the temporary storage area to the version library, the command is as follows
bashcopy code
git commit -m '提交的内容说明'
-
Graphic:
It is equivalent to archiving once, generating a commit record in the repository (and generating a version number)
This archive will not delay us to continue writing projects under the workspace (project folder)
Git log and status view
-
To view all submitted log records, the command is as follows
bashcopy code
git log
The effect of running the command is as follows:
-
When we have more and more logs, we may want to simplify the viewing, we can enter the following command
--oneline display brief information on one line
copy code
git log --oneline
The effect of running the command is as follows:
-
If you have changed too much code and forgot which ones have been changed, you can run the following command to view the changes in the git warehouse, and you can only see the status of all uncommitted changed files
bashcopy code
git status
The effect of running the command is as follows:
-
Temporary storage, and submit again to generate a version record
bashcopy code
git add . git commit -m '新建登录页面_和样式'
-
The diagram is as follows:
Git version rollback
Time machine, go back to the past~
-
The fallback command syntax is as follows, Chinese depends on the situation
bashcopy code
git reset --hard 版本号
-
View version number
The version number is randomly generated each time, ours is definitely different
copy code
git log --oneline
-
Try rolling back
bash477321b
to this recordcopy code
git reset --hard 477321b
-
Observe the workspace, the rollback is successful
-
If you want to go back to the record of the latest submission, you will find that git log cannot see future records, but the problem is not big
Enter the git reflog command to view all git operation records, including your reset records
bashcopy code
git reflog
The effect of running the command is as follows:
Extended command:
- git bash (terminal) clears the screen:
clear
- git bash (terminal) starts a new page:
Ctrl + L
Git ignores files
Sometimes, we don't want git to track and manage certain files/folders. In this scenario, we need to create an ignore file called .gitignore (fixed name) next to the .git folder, and write the ignore rules. The file name here is .gitignore, which is not a suffix.
The project folder structure is as follows:
gitignore
copy code
# .gitignore内容: password.txt 其余用法: # 忽略文件夹 css # 忽略文件夹下的某个文件 css/index.js # 忽略文件夹下某类文件 css/*.js
Create a new password.txt in the root directory to see what changes git has tracked
bash
copy code
git status
The effect of running the command is as follows:
It was found that only .gitignore was added, and those that conformed to the rules were ignored
Git branch
branch nature
- A branch is actually a pointer mark called HEAD . Every time the code is submitted, the HEAD pointer will move back once to ensure that what it points to (and in the workspace) is the last submission. When we hit the command:
git reset --hard a3bcab2
, the HEAD pointer will move, and after HEAD moves, it will affect the code in the workspace
create branch
-
The command to create a branch is as follows
bashcopy code
# 创建分支 git branch 分支名
-
Note: Branches will not be automatically switched after creation, we can run the command to view all branches in the current .git repository
The command to view all branches of the current version library is as follows
bashcopy code
# 查看当前版本库所有分支 git branch # 绿色带*代表, 你现在所处的分支
The effect of running the command is as follows:
-
Manually switch to the branch
Note: Create and switch to this branch for the first time, here you will find that all the codes on the master branch (and all submission records of the current node) have been copied, on this basis, you can develop later
copy code
# 切换分支命令 git checkout 分支名
The effect of running the command is as follows:
-
The diagram is as follows:
Branch development process
-
We can now write under the current reg branch, register the logic code of the page, for example, create a new reg.html file, and write something casually
-
Then temporarily store and submit once, and the record submitted this time will appear here, as shown in the figure
- Therefore, in the future, when developing under the current reg branch, a version record will be generated for each submission within the reg range, but it will not affect other branches
branch merge
-
We can merge the code written in the branch into the main branch/other branches
-
First, switch to the target branch you want to merge into, here is the master main branch as an example
bashcopy code
# 切换分支 git checkout master
-
The diagram is as follows
-
Merge Command Syntax
bashcopy code
# 把目标分支名下的所有记录, 合并到当前分支下 git merge 目标分支名
-
Here we execute the command
bashcopy code
git merge reg
-
Effect diagram after execution
branch deletion
-
If the registration function is developed and the code has been merged into the master branch, we do not need the reg branch
-
The command is as follows
bashcopy code
git branch -d 分支名
-
If your current branch code has not been merged into other branches, an error will be reported
Conflicts when merging branches
In the two branches, the same file has been modified and submitted. When merging, conflicts will occur
-
Simulate a simple conflict here
-
Under the master branch, modify a line of code in login.html, and complete a temporary submission
-
Switch to the reg branch, modify the corresponding line of code in login.html, and complete a temporary submission
-
-
Then switch back to the master branch, use the merge command to merge the code and changes under the reg branch
Not surprisingly, there will be conflicts
-
After a conflict, the VSCode interface
At this point we have to make a choice: adopt current changes, adopt incoming changes, keep all
-
After selecting the retention method, you need to temporarily save and submit again
At this point, the conflict state is ended and it returns to the normal state
-
Print log records after conflicting merges
Summary: When we encounter a conflict in the merge, we should resolve it manually, then store it temporarily, and submit it once
Supplement - Detailed explanation of Git branch flow chart
- HEAD head pointer, which points to the submission record will be overwritten to the work area and temporary storage area, each submission will generate a new record, master and HEAD will be moved backward
-
Optimistic about the current branch and records, and create a new branch based on this node (including all previous submission records),
git branch reg
a pointer mark will be created to identify the reg name pointing to
-
git checkout reg
The switch is pointed to by the HEAD pointer (switch branch)
-
After the registration page is created,
git add .
it is added to the temporary storage area, andgit commit -m
a submission record is generated
-
After the style of the registration page is newly created, the submission is temporarily stored, and a submission record is generated
-
Merge branch (commit record)
- Goal: You want to merge A into B. For example: Merge reg into master
- git checkout B, after switching to the target branch
- git merge A, merge the A branch records into the B branch
First switch to the main branch git checkout master
-
Merge reg branch git merge reg
-
Xiao Ming - modified the index.html file, under the reg branch, and temporarily saved and submitted it, resulting in a record
-
Xiao Ming - modified the index.html file (the same file), under the master branch, and temporarily saved the submission, and generated a record
-
Merge the reg and change the same file to report an error, which needs to be resolved
-
After manually resolving conflicts, a new commit record will be generated
-
Delete the reg branch and the whole process is over
Git remote repository
introduce
A remote repository is a repository of your project hosted on the Internet or other network that stores all records and archives of our repository. Support multi-person collaboration and jointly manage remote warehouses, so that when our computer is broken, we can also clone a previously submitted code from the remote warehouse to continue development locally
The mainstream remote warehouses include GitHub (gay hub), the world's largest peer-to-peer dating community, and gitee (code cloud), whose server is in China. Since the GitHub server is abroad, it is necessary to surf the Internet scientifically or use other acceleration tools. For convenience, here is Code Cloud as an example for beginners’ reference. The GitHub process is similar to gitee
register
After registering and logging in to the gitee.com website, add the main email address as the email address set in your local git warehouse. Note that it must be the same, otherwise it cannot be submitted correctly. If you forget the email address set locally, you can open the console and enter to git config --list
check the email address again. Of course, you can also use git config --global user.email 你的邮箱地址
the
In the email setting interface, do not check the box that does not disclose my email address, otherwise it will not be able to submit normally
new warehouse
You can choose to create a remote warehouse project (multiple), the creation interface is as follows
After the check is complete, select Create. After creation, you will get a link to the address of the remote warehouse, which usually ends in .git.
Addresses fall into two of the most commonly used transport protocols:
- HTTPS protocol: need to enter username and password gitee.com/(userName)/…
- SSH protocol: you need to configure the secret key, you can log in to [email protected] without password : userName/repositoryName.git
Select the SSH path, the interface is as follows
ssh configuration
We can log in without password after configuring SSH on this machine once
-
ssh key composition and function
textcopy code
ssh key 的作用:实现本地仓库和 gitee平台之间免登录的加密数据传输 ssh key 由两部分组成,分别是: id_rsa(私钥文件,存放于客户端的电脑中即可) id_rsa.pub(公钥文件,需要配置到 gitee平台 中) 私钥加密的信息,只能通过公钥解密。公钥加密的信息,只能通过私钥解密。安全性高!
Create and use steps:
-
First generate a secret key on the local machine (it can also be regenerated and reconfigured later), open a terminal at random, and enter the following command:
bashcopy code
ssh-keygen -t rsa -C "你注册账号的邮箱"
-
Press Enter three times in a row to generate two files, id_rsa and id_rsa.pub, in the C:\Users\username folder.ssh directory
-
Use vscode to open the id_rsa.pub file and copy the text content inside
-
Paste the configuration into Code Cloud -> Settings -> ssh public key
-
If it is a mac, you can enter the following tutorial to view: mac obtains the public key
Initialize an empty repository
First configure the address of a remote warehouse for the local warehouse, and establish a link between the warehouses
Since each push operation needs to bring the address of the remote warehouse, it is very troublesome. We can set an alias for the warehouse
bash
copy code
# 给远程仓库设置一个别名 git remote add 仓库别名 仓库地址 git remote add origin [email protected]:(username)/repository.git # 删除 origin 这个别名 git remote remove origin # 使用 -u 记录 push 到远端分支的默认值,将来直接 git push 即可 git push -u 仓库别名 分支名
The following is an example of actual operation:
-
Just create a new project folder, initialize git, and then fill in some content in the project file, here I add a new
.gitignore
file, and then temporarily save and submit it to the local git repository -
Enter the following command:
bashcopy code
# 注意:这里的existing_git_repo是你的项目根路径 cd existing_git_repo #如果你是在项目文件夹开启的终端,忽略此行 # 添加远程仓库关联,仓库别名origin,可以随意更改,后接ssh地址 # 此处的ssh是自动生成的,可以去gitee空仓库的代码页直接复制即可 git remote add origin [email protected]:li-houyi/test-factory.git # 第一次推送到远程时需要指定具体的分支,因为远程仓库并没有这个分支 # 使用 -u 记录 push 到远端分支的默认值,将来直接 git push 即可 git push -u origin "master"
-
This page is considered successful:
Note: The pushed local warehouse must be non-empty and the local temporary storage has been submitted, otherwise an error will be reported! This is also easy to understand. If you send an empty project to an empty warehouse, shouldn’t this give you an error?
-
After the push is successful, re-enter the gitee warehouse page to check whether the push is correct
-
After the empty warehouse is successfully created, you can open the warehouse on the management page, of course, you can not set open source (default private)
native no project - clone
If you don't have a project locally, and if you want to get the code from someone else's warehouse or your own warehouse, you need to clone the project
-
The command is as follows:
bashcopy code
git clone 目标远程仓库的git地址
-
The process is as follows:
-
If the project has only one branch, then the above code has been cloned after execution (git clone pulls the master branch by default), but in actual development, there is not only one branch, so we need to perform the following steps:
- Build a branch locally, the branch name is the same as the remote branch name, check the remote branch name using
git branch -r
copy code
git checkout -b 对应远程分支名
- Pull the remote branch ( do not directly pull the code of the corresponding branch in the master branch, switch to the newly created branch )
copy code
# 每次拉取都需要指定远程仓库名和分支名 git pull 远程仓库名 分支名
- Note: The above 2 lines of commands can be combined into one line
copy code
git checkout -b 分支名 origin/分支名
- Extension: Set the branch pulled by git pull by default
copy code
git branch --set-upstream-to=origin/远程分支名 本地分支名
- Build a branch locally, the branch name is the same as the remote branch name, check the remote branch name using
Collaborative development
- A writes code, stashes, submits, and then pushes to the remote server
- B writes the code, temporarily saves it, and submits it. You can first pull the record just submitted by the other party to the local merge, and then push it to the remote after the merge is correct.
- A can git pull to pull the latest remote code version to the local merge
Either A or B here can go first, continue to pull the updated code, just use the git pull command
Review of Git remote warehouse process
-
Step1:
-
Step2:
-
Step3:
-
Step4:
Overview of Git common commands
Author: Yaonan.
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, for non-commercial reprint, please indicate the source.