Summary of common commands of Git and Gitlab

Table of contents

1. The four working areas of Git

Two, Git workflow

Three, Git common commands

(1) Project creation and initialization

(2) Local branch management

(3) Code version management

4. Common errors and solutions

(1) Commit-related errors


1. The four working areas of Git

1. Workspace

Operations such as storing project codes, adding, deleting, and modifying files or codes all take place in the workspace.

2. Index / Stage (temporary storage area)

It is used to store the changes added by the workspace, and all local changes can be stored in the temporary storage area by using git add.

3. Local Repository (local warehouse)

It is used to store the changes submitted in the workspace and the temporary storage area. Using git commit, the modified files added to the temporary storage area can be submitted to the local warehouse. The local warehouse is also the hidden .git file in the workspace directory, which contains the data of all versions after you submit, where HEAD points to the latest version put into the warehouse.

4. Remote Repository (remote warehouse)

A remote server for hosting code, using git push to push the modified content stored in the local warehouse to the remote warehouse.

Two, Git workflow

  1.  Add, modify, delete files in the workspace (coding process);
  2.  Add the change files that need to be submitted to the temporary storage area;
  3.  Submit the files in the temporary storage area to the git repository.

Therefore, the files managed by git have three states: modified (modified), staged (staged), and committed (committed).

Three, Git common commands

(1) Project creation and initialization

1. git init initializes the new project and makes the first submission

git init
git add .
git commit -m "第一次提交"
git remote add origin 仓库地址
git push origin master

2. git clone clones remote warehouses of existing projects

git clone 仓库地址

(2) Local branch management

1. View all branches

git branch

2. Create a new branch (from the current branch)

git branch 新分支名

During the development process, you need to create your own development branch for development, and do not submit on the main branch until the test is completed

3. Switch to a branch

git checkout 分支名

For example: git checkout master, that is, switch to  master the main branch, the current branch is the main branch

4. Create a new branch (from the current branch), and switch to the new branch (combination of 2 and 3)

git checkout -b 分支名

5. Merge a branch with the current branch

(1) The branch to be merged is a local branch

git merge 要合并到的本地分支名

(2) The branch to be merged is a remote branch

git merge origin/要合并的远程分支名

Common scenarios at work:

(1) When the code on the development branch passes the test and reaches the online standard, it needs to be merged into the master main branch

# 本地拉取开发分支,保证本地是最新内容
git checkout 开发分支名
git pull
# 切换到主分支下
git checkout master
# 保证获取主分支最新内容
git pull
# 将开发分支合并到主分支上
git merge 开发分支名
# 合并完需要再将主分支推送到远程仓库
git push -u origin master

(2) When the code on the development branch passes the test and reaches the online standard, it needs to be merged into the master main branch, but only part of the submissions on the development branch are expected to be merged into the main branch, and all submissions are not expected to be merged.

# 本地拉取开发分支,保证本地是最新内容
git checkout 开发分支名
git pull
# 切换到主分支下
git checkout master
# 保证获取主分支最新内容
git pull

# 确定需要从开发分支合并到主分支的提交,查看提交记录
git checkout 开发分支名
git log --oneline
# 记录下你想要合并的提交ID,例如:a123456、b234567等。

# 切换回主分支,并使用cherry-pick命令将选定的提交应用到主分支上
git checkout master
git cherry-pick a123456 b234567

# 如果遇到合并冲突,请解决冲突,并继续执行 cherry-pick 命令
git add 解决完冲突的文件及路径
git commit -m "解决合并冲突"
git cherry-pick --continue

# 最后将主分支推送到远程仓库
git push -u origin master

(3) When the master code is changed, the code on the development branch needs to be updated

# 先切换到主分支,并拉取最新代码
git checkout master 
git pull
# 切换到开发分支
git checkout 开发分支名
# 将主分支合并到开发分支
git merge master 
# 将最新的开发分支提交到远程仓库
git push -u origin 开发分支名

6. View the status of the branch workspace

# 查看当前分支工作区是否有未跟踪的文件(即是否有未添加到暂存区的文件)
git status
# 查看当前分支工作区的文件与暂存区文件的差异
git diff

(3) Code version management

1. git pull pulls the latest code of the specified branch

(1) Pull the latest code of the specified branch and merge it into the local branch (both of the following are available)

# 方法1
git pull origin 要拉取的分支名
# 方法2
git checkout 要拉取的分支名
git pull

(2) Pull the latest code of the specified branch, but not merge into the local branch (both of the following are available)

# 方法1
git fetch origin 要拉取的分支名
# 方法2
git checkout 要拉取的分支名
git fetch

2. git add adds the specified directory to the temporary storage area

(1) Add all files in the current directory to the temporary storage area

git add.

(2) Add the specified directory to the temporary storage area (including its subdirectories)

git add [dir]

(3) Add the specified file to the temporary storage area

git add [fileName1] [fileName2] ...

3. git commit Submit the contents of the temporary storage area to the local warehouse

git commit -m "提交内容说明"

4. git push pushes the content of the local warehouse branch to the origin remote warehouse

git push origin 本地分支名

5. Undo the submitted changes

(1) git reset: Roll back the current HEAD (the latest version put into the warehouse) to the specified commit version, and all commits after the commit will be cleared

# 【保留工作区,保留暂存区、修改本地仓库】
git reset --soft

# 【保留工作区、修改暂存区、修改本地仓库】(git reset默认的模式)
git reset --mixed

# 【修改工作区、修改暂存区、修改本地仓库】
git reset --hard 

(2) git revert: Create a new commit to overwrite the current commit, move the pointer backwards, and realize the withdrawal function.

  • git revert Afterwards, there will be one more item  commit, HEAD is forward, and the withdrawal operation can be performed
  • git reset Delete the previous one directly  commit , and HEAD rolls back

4. Common errors and solutions

(1) Commit-related errors

Error:

✖   subject may not be empty [subject-empty]
✖   type may not be empty [type-empty]
✖   found 2 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
husky - commit-msg hook exited with code 1 (error)

solution:

Since the project uses husky, the code specification was verified before submission, which resulted in an error. When committing, add the submission information: "fix: xxxxx", for example:

git commit -m "fix: 初始化项目"

Error:

eslint --fix found some errors. Please fix them and try committing again.

solution:

git commit --no-verify -m “跳过检查提交”

Guess you like

Origin blog.csdn.net/qq_45397526/article/details/130154447