Detailed operation of git

01 Introduction to Git

Target

Master the basic definition of git and

Gitwhat is

  • GitThe version control system is a distributed system, a tool used to save the historical state of the project source code (game archive)命令行
  • Git is a command line (little black window) tool for version control (archiver)

What is the role of Git?

    • 版本管理Tool: Speaking human words can record every link of your typing code (similar to playing game archives)

Introduction to Git Application Scenarios

  1. 多人开发code management

At present, when we have multiple people code, we want to merge the code together by using the most primitive copy and paste operation. With git, you can do it with one click

  1. 异地开发code management

In actual development, we may use the company computer to type code at work. Sometimes when I go home and use my computer to work overtime secretly, the previous method is to prepare a USB flash drive and copy and paste. With git, it can be done directly with one click.

  1. 版本管理
    For example, I have made version 1.0 of my company website and am using it. Now I plan to add some new functions, but this function is unstable and needs to go through development and testing, so as not to affect the existing stable version. The previous practice was to make a separate copy of the stable version to add functions, and then replace the previous version after it was done. With git, it can be done with one click.
  2. 版本回滚:
    For example, this week the product manager proposed a function, and I worked hard to write the code for a week.
    Next week, the product manager said that this function was no longer available, and asked me to delete it and replace it with other functions.
    After we deleted the code, we worked hard to write the code for another week according to the requirements of the product manager. Next week, the product manager said that this function is still not as good as last time, let's change it to last week... Don't

    worry, after you have git, you can get it done with one click.

Are there any other version management tools?

  • git: currently the most used version management tool
  • svn: Some companies are also using it, and the usage is similar to git (equivalent to Tencent Video and Youku Video)

summary

1. What is git?

Code version management software necessary for programmers

02Git installation process

Target

Master the installation process

download

Address: Git - Downloads

Install

Just click the whole process by default, no need to modify other options ( suggestion: do not install in a location other than the C drive )

Check if the installation is successful

If you right-click any folder on the computer, the Git command option can appear, indicating that the installation is successful

mac system

1. First use the built-in terminal, enter: git --version , and press Enter if you see the version number. Description has been installed

2. If you do not see the version number, you can enter in the terminal: brew install git , then press Enter and wait for the installation. After the installation is successful, repeat step 1 to check whether the installation is successful

03 Configure user information

Target

Master the way to configure user information

Why configure

After installing the git software, the first thing to do is to set up your own 用户名and 邮件地址so that we can know who submitted what code.

Configure user information commands

git config --global user.email "Your code cloud account email"

git config --global user.name "Your code cloud user name (you can write whatever you want)"

Notice:

  1. The user name and email address configured by git config --global user.name and git config --global user.email will be written to the C:/Users/username folder/.gitconfig file. This file is Git's global configuration file, which can take effect permanently after being configured once.
  2. The email address and user name above may not be real, or you can modify it later

View all global configurations

git config --list --global

View all global configurations

git config --list --global

View the specified global configuration items

git config user.name

git config user.email

04 The basic working process of using git

Target

Master the basic workflow of git

9 common operations

There are 9 high-frequency operations for programmers. details as follows.

  • 1. Create a new project folder (only once)
  • 2. Enter the folder (important)
  • 3.初始化仓库:git init(only do it once)
  • 4.编码
  • 5.添加文件信息: git add .
  • 6.确认添加信息:git commit -m"描述信息"
  • 7. View detailed log information: git log
  • 8. View brief log information: git log --oneline
  • 9. Version rollback: git reset --hard version number

Step 123 is only needed once for a project, step 456 is used frequently, and step 789 is used occasionally

1. Create a new folder

If it is an old project (an existing project), this step can be omitted.

2. Start to enter the git command

Enter this folder, right click and select Git Bash Here

Open the git command line tool

(small blue window)

3. Initialize the project

Enter in the small blue windowgit init

Notice:

  1. If it is an old project, this step is omitted.
  2. The .git directory may be hidden and needs to be set to visible to see it

4. Coding (daily work of programmers)

Do normal development in this directory: create files, modify file content....

For example: add index.txt, and write some content (any content is fine)

5. Add archive

Order:

git add .

Note: This command just adds the file information to the temporary storage area, and it has not been archived yet. You need to use the git commit command to confirm the archive.

6. Confirm archive

Ordergit commit -m "说明"

icon

以上流程是实际开发中主要使用流程

summary

git add .

git commit -m "operation content"

05 Three areas of git

Target

Understand the role of the three areas of git, and master the usage of git add commit

three districts

  • workspace: the area where work is done
  • Temporary storage area: an area for temporary storage
  • Local git warehouse: final storage area

Comparative understanding

Life

folder

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

reflected in the code

Three zones and git commands

06 View submission information

Target

Master common commands for viewing commit information

View commit history

The following two commands are used to view the submission records

  • git log
  • git log --oneline
  • git reflog: This command can view all your submitted records

07 View file status

Target

Master the command to view the status of the file, and be able to understand the 4 statuses of the file

content

Files not being hosted: not tracked

Files that have been hosted (add, commit): modified, staged, submitted

Order

# Check the status of the warehouse file
git status

# View short version information
git status -s

After changing the file:

Newly added files:

  • Untracked (added files)
  • Modified (the file was once recorded by Git , and then modified in the workspace)
  • Temporary (newly added files, the state after being added to the temporary storage area)
  • Submitted (nothing to commit)
    • Indicates that nothing can be submitted; that is, all content has been submitted to the git repository.
    • Some documents also refer to this state as unmodified , which means that the code has not been modified since the last submission

practise

Operate the file by yourself, and you can see 4 states.

08 code recovery

Target

Master the method of code recovery

restore from workspace

I changed the code and want to give up the modification (there is no add at this time)

git status

git restore filename

Restore from staging area

I changed the code and added it, so I want to give up at this time.

git reset HEAD filename

git checkout --filename

overall rollback

after rollback

git reset --hard 提交流水号

It will lead to incomplete log information. Use git reflog instead

08 file_git ignore file

Target

Understand the role and configuration of git ignore files

explain

  1. Sometimes, we don't want git to track and manage certain files/folders
  2. In this scenario, we need to create an ignore file called .gitignore (fixed name) next to the .git folder , and write the ignore rules
  3. These rules can be written as follows
# The hash sign indicates a comment
# ignore a specified file
password.txt

# ignore folder
css

# ignore a file in the folder
css/index.js

# Ignore certain types of files in the folder
css/*.js

  1. Then create a new password.txt in the root directory to see what changes git has tracked
git status

  1. It was found that only .gitignore was added, and those that conformed to the rules were ignored

summary

  1. How to set git to ignore files?
  • Create a .gitignore file next to the .git folder, and write the relevant syntax configuration to ignore the folders/files that meet the rules under this warehouse folder

09 Remote Warehouse Introduction

Target

What is a remote warehouse

The remote (remote) warehouse is actually a remote git server, a server that helps us store code.

  • Local git storage: Once your computer is lost or the hard disk is damaged, just go straight to the street
  • Remote git storage: If your computer is broken and replaced with a new one, you only need to re-download it from the Internet

What are the common remote warehouses?

(1) github: free. Access to foreign servers is unstable. "The World's Largest Gay Dating Site"

    • It can be used when studying, and some small companies will also use it to save money at work

(2) Code Cloud: Free. domestic server.

(3)gitlab:

(4) Private server: the server in the company's own computer room (encountered at work)

The above remote warehouse usage process is almost the same.

The role of remote warehouse

Collaboration

The use process of remote warehouse

leader (the creator of the warehouse, has the highest authority)

Log in to the remote warehouse and create an empty project;

Add permissions to members

member

  1. Log in to the remote warehouse
  2. Pull the code locally
  3. Edit locally, save, submit code
  4. sync to remote

summary

10 Examples of Remote Warehouse Usage

Target

Take Code Cloud as an example to master the use of remote warehouses

1. On the code cloud website

建立仓库

2. Put the remote warehouse克隆到本地

 git clone 仓库网址

git clone: ​​clone, clone the entire remote warehouse to the local

    • This naming is equivalent to pulling all the code (copy the entire remote warehouse folder to your local)
    • This command is generally only available 项目开始when执行一次

It will create the directory locally.

3.本地编写代码

git add . is the same as git commit -m "add file info"

4. Put the local code推送到远程

git push

5. Put the remote code拉取到本地

git pull: Pull, used for multi-person development. For example, if a code file written by someone else is submitted to a remote warehouse, the remote warehouse code can be pulled to the local through this command

  • This command will not pull all the code, just pull the newly added code on the remote (merge the code written by others into your local)
  • 每天This command is recommended to be executed before going to work when multiple people are developing一次

11 Remote warehouse multi-person development and configuration process

Target

Master the basic process of multi-person development

content

  1. The leader (creator of the warehouse) invites other developers Xiaohua
  2. Xiaohua becomes a developer
  3. Xiaohua clone code
  4. Xiaohua normal development project
    1. git add .
    2. git commit -m ""
  1. Xiaohua submitted the code
    1. git push

overall icon

Common mistakes with git pull

1. Remember to commit before pull

  1. merge conflict

Merge: Merge the two versions of the code together.

Conflict: Merge failed.

For example:

  1. Student A changed the first line of a.txt and submitted it to the remote;
  2. Classmate B's local warehouse also changed the first line of a.txt and committed
  3. Classmate B's git pull (pull from the remote) made an error: two people changed the same place.

deal with:

  1. Solve the problem manually. see what code to use
  2. add commit

  1. git pull
  2. git push

12 remote warehouse SSH configuration

Target

Master the SSH configuration method of the remote warehouse

two access methods

There are two ways to access the remote warehouse, which are HTTPSwithSSH

  • HTTPS: 零配置; But every time you visit the warehouse, you need to repeatedly enter the account number and password to access successfully (not needed now)
  • SSH: 需要进行额外的配置; But after the configuration is successful, there is no need to repeatedly enter the account number and password every time you access the warehouse. It is also recommended to use SSH to access remote warehouses in the company.

Introduction to SSH

encrypted communication protocol

The role of SSH key: to realize the encrypted data transmission between the local warehouse and Github without login.

The benefits of SSH key: login-free identity authentication, encrypted data transmission.

SSH key consists of two parts, namely:

  • id_rsa (private key file, stored in the client computer)
  • id_rsa.pub (public key file, which needs to be configured in the remote server)

SSH configuration process

  1. Open Git Bash
  2. input the command:ssh-keygen -t rsa -C '邮箱'
    1. The mailbox here should be consistent with the global configuration (git config --global user.email) and the registered mailbox on Code Cloud
    2. 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
  1. Open the id_rsa.pub file with Notepad and copy the text inside

  2. In your remote repository url,点击头像 -> 设置 -> SSH 公钥 -> 粘贴

work process

summary

11 Branch Introduction

problem import

For example, the first version of your website has been released, and its functions are stable. Now I am going to add some new functions to develop version 2.0. The question is: how to write code?

If you continue to develop on the basis of version 1.0, how to ensure that the code of 1.0 is stable? Programmer's approach: cut branches.

Graphic:

understanding branch

A branch is what it is: a link to a version.

  1. What is the function of branch?
  • It can independently develop and maintain the submitted version records, and does not affect each other with other branches, especially when multiple people develop the same project, they can control their own code separately
  1. After the function development in the branch is completed, what should be the last step?
  • Merge back to the main branch to ensure that the final main branch has the code of all modules, and finally deliver it uniformly

Branch application scenarios

  1. Cut branches according to functional modules during development
    1. For example: there are 5 pages, cut 5 branches
  1. When there is a bug, split the branch to solve the bug

git branch related commands

Git命名

作用

详细描述

git branch

view current工作分支

git branch branch name

new sub-branch

Create a new trumpet archive and back up the current archive to the trumpet

git checkout branch name

switch working branch

Switch the current account (change the trumpet to brush equipment)

git merge branch name

merge branch

Merge the sub-branch code into the main branch (the equipment from the small account is moved to the large account)

git branch -d branch name

Delete sub-branches (use with caution)

delete a branch

12. Typical operations related to branches

1. View branch

Order:git branch

Function: view all branches

Note: At the beginning of the project, there will be a default branch named: master. Indicates the master branch.

2.新建分支  

Order:git branch 分支名

Function: create a new branch

Example:git branch v2

Graphic:

3.切换分支

Command: git checkout 分支名(After creating the branch, your code is still in the main branch by default, you need to switch the working branch to the sub branch)

Function: switch branch

Example:git checkout v2

Graphic:

Note: When switching branches, commit first
 

4.在新的分支上进行编码

When the code is submitted, the pointer is also moving

// Change the code

git add .

git commit -m "commit 3"

// Change the code

git add .

git commit -m "commit 4"

5.合并分支

Format: git merge branch name

Note: Before merge, you must commit

Merge: Assuming that it is currently in the master branch, merge the code of the v2 branch. The purpose of merging: the code in master is the same as the code in v2. Let the master also have the v2 code.

6.删除分支

Format:git branch -d 分支名

Example: git branch -d v2

Notice:

  1. To delete a branch, you also need to switch the current working branch to another branch
  2. Delete a branch without losing code

Guess you like

Origin blog.csdn.net/bai101724/article/details/128423852