Git from installation to basic use (nanny tutorial 2023)

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 

image-20230612100943906.png

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 

image-20230612101818017.png


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:

image-20230612154411204-1687173588607.png

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:

image-20230612130038686.png

 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: 

image-20230612130350943.png

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 

image-20220625155941259.png

右侧没有标记的时候为“未修改” 或 此文件/文件夹, 被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 

image-20230612141325305.png

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

  1. To change the workspace and put it in the temporary storage area, the following command

    bash

    copy code

    # (了解) 将index.html 添加到暂存区 git add index.html # 将css目录下一切添加到暂存区 git add css
  2. 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 stored

    bash

    copy code

    git add .
  3. Submit the content of the temporary storage area to the version library, the command is as follows

    bash

    copy code

    git commit -m '提交的内容说明'
  4. 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)

image-20230612154411204-1687173588607.png

Git log and status view

  1. To view all submitted log records, the command is as follows

    bash

    copy code

    git log

    The effect of running the command is as follows:

image-20230612212059053.png

  1. 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

    bash

    copy code

    git log --oneline

    The effect of running the command is as follows:

image-20230612212137406.png

  1. 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

    bash

    copy code

    git status

    The effect of running the command is as follows:

image-20230612212456778.png

  1. Temporary storage, and submit again to generate a version record

    bash

    copy code

    git add . git commit -m '新建登录页面_和样式'
  2. The diagram is as follows:

image-20230612154411204.png

Git version rollback

Time machine, go back to the past~

  1. The fallback command syntax is as follows, Chinese depends on the situation

    bash

    copy code

    git reset --hard 版本号
  2. View version number

    The version number is randomly generated each time, ours is definitely different

    bash

    copy code

    git log --oneline

image-20230612212700778.png

  1. Try rolling back 477321bto this record

    bash

    copy code

    git reset --hard 477321b
  2. Observe the workspace, the rollback is successful

    image-20230612212808682.png

  3. 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

    bash

    copy code

    git reflog

    The effect of running the command is as follows:

image-20230612212857923.png

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:

image-20230612213354387.png

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:

image-20230612214049685.png

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

  1. The command to create a branch is as follows

    bash

    copy code

    # 创建分支 git branch 分支名
  2. 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

    bash

    copy code

    # 查看当前版本库所有分支 git branch # 绿色带*代表, 你现在所处的分支

    The effect of running the command is as follows:

image-20230612215435954.png

  1. 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

    bash

    copy code

    # 切换分支命令 git checkout 分支名

    The effect of running the command is as follows:

image-20230612215712754.png

  1. The diagram is as follows:

image-20230612215806700.png

Branch development process

  1. 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

  2. Then temporarily store and submit once, and the record submitted this time will appear here, as shown in the figure

image-20220626005643763.png

  1. 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

  1. We can merge the code written in the branch into the main branch/other branches

  2. First, switch to the target branch you want to merge into, here is the master main branch as an example

    bash

    copy code

    # 切换分支 git checkout master
  3. The diagram is as follows

image-20230612220823875.png

  1. Merge Command Syntax

    bash

    copy code

    # 把目标分支名下的所有记录, 合并到当前分支下 git merge 目标分支名
  2. Here we execute the command

    bash

    copy code

    git merge reg
  3. Effect diagram after execution

image-20220626011937230.png

branch deletion

  1. If the registration function is developed and the code has been merged into the master branch, we do not need the reg branch

  2. The command is as follows

    bash

    copy code

    git branch -d 分支名
  3. 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

  1. Simulate a simple conflict here

    • Under the master branch, modify a line of code in login.html, and complete a temporary submission

      image-20230612221800522.png

    • Switch to the reg branch, modify the corresponding line of code in login.html, and complete a temporary submission

      image-20230612222009473.png

  2. 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

image-20230612222117265.png

  1. After a conflict, the VSCode interface

    image-20230612222156560.png

    At this point we have to make a choice: adopt current changes, adopt incoming changes, keep all

  2. After selecting the retention method, you need to temporarily save and submit again

    image-20230612222444912.png

    At this point, the conflict state is ended and it returns to the normal state

  3. Print log records after conflicting merges

    image-20230612222705106.png

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

image-20230612224348269.png

  • Optimistic about the current branch and records, and create a new branch based on this node (including all previous submission records), git branch rega pointer mark will be created to identify the reg name pointing to

image-20230612224959233.png

  • git checkout regThe switch is pointed to by the HEAD pointer (switch branch)

image-20230612225147670.png

  • After the registration page is created, git add .it is added to the temporary storage area, and git commit -ma submission record is generated

image-20230612225547703.png

  • After the style of the registration page is newly created, the submission is temporarily stored, and a submission record is generated

image-20230613003559070.png

  • 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

image-20230613004008281.png

  • Merge reg branch git merge reg

image-20230613004234355.png

  • Xiao Ming - modified the index.html file, under the reg branch, and temporarily saved and submitted it, resulting in a record

image-20230613004635286.png

  • Xiao Ming - modified the index.html file (the same file), under the master branch, and temporarily saved the submission, and generated a record

image-20230613010644386.png

  • Merge the reg and change the same file to report an error, which needs to be resolved

image-20230613010950109.png

  • After manually resolving conflicts, a new commit record will be generated

image-20230613011347885.png

  • Delete the reg branch and the whole process is over

image-20230613011445775.png

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

image-20230615093344985.png

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 --listcheck 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

image-20230615094323926.png

new warehouse

You can choose to create a remote warehouse project (multiple), the creation interface is as follows

image-20230615095044748.png

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:

Select the SSH path, the interface is as follows

image-20230615095628604.png

ssh configuration

We can log in without password after configuring SSH on this machine once

  • ssh key composition and function

    text

    copy 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:

    bash

    copy 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 仓库别名 分支名

image-20230615101817727.png

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 .gitignorefile, and then temporarily save and submit it to the local git repository

    image-20230615103357225.png

  • Enter the following command:

    bash

    copy 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:

    image-20230615104040319.png

    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

    image-20230615105531979.png

  • 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:

    bash

    copy code

    git clone 目标远程仓库的git地址
  • The process is as follows:

image-20230615110423501.png

  • 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 usinggit branch -r
    bash

    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 )
    bash

    copy code

    # 每次拉取都需要指定远程仓库名和分支名 git pull 远程仓库名 分支名
    • Note: The above 2 lines of commands can be combined into one line
    bash

    copy code

    git checkout -b 分支名 origin/分支名
    • Extension: Set the branch pulled by git pull by default
    bash

    copy code

    git branch --set-upstream-to=origin/远程分支名 本地分支名

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:

image-20230616072225869.png

  • Step2:

image-20230616072307518.png

  • Step3:

image-20230616072523621.png

  • Step4:

image-20230616072547086.png

Overview of Git common commands

image-20230620223216096.png

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.

Guess you like

Origin blog.csdn.net/YN2000609/article/details/131919082