[Git] Summary of git common commands

Briefly

Git is an open source distributed version control system that can effectively and quickly handle version management from small to very large projects.
There are a lot of commonly used command syntax in it, here is a summary record of common commands, in case of emergency.

command summary

由于git是基于linux开发的工具,所以有个特点:不报错即成功。
一些关于linux的命令操作有的也能用。

Example of error reporting:
insert image description here

git configuration management

安装git后,在桌面右键打开命令工具,然后进行git配置

insert image description here

Add username and email

  • set username
//	全局设置姓名
git config --global user.name "your_username"

  • set email
//	全局设置邮箱
git config --global user.email [email protected]
  • View git configuration
//	查看配置
git config --list 查看所有配置

generate ssh

  • Generate the ssh key and press Enter all the way.
ssh-keygen -t rsa -C "[email protected]"

After the key is generated, you can find the public key generated by ssh in the user on the c drive, and paste it to the personal user ssh management on the remote website of the git project.
insert image description here

Check the git version

git version

git -v

git command help

git help

git -h
//	打开网页版文本
git -h config

Project git management

Initialize local project

  • Generate project git repository locally
git init 

Connect to remote project

  • Connect to the remote warehouse, origin defaults to the remote address name, which can be customized
git remote add origin 远程仓库地址
  • View the remote warehouse address set
//	远程仓库地址名
git remote
//	详细
git remote -v
  • Remote warehouse address name renaming
git remote old仓库名 new仓库名
  • Remote warehouse address name deletion, once you delete a remote warehouse in this way, all remote tracking branches and configuration information related to this remote warehouse will also be deleted together.
git remote remove 仓库地址名
//	简写
git remote rm 仓库地址名

clone project

  • To clone the project file according to the remote warehouse address, you must first right-click to open the git command tool
git clone 远程仓库地址
//	克隆指定分支的代码
git clone -b 分支名 远程仓库地址

Warehouse code git management

branch operation

  • View repository branch
git branch 
//	查看分支的最后一次提交
git branch -v
  • new branch
git branch 分支名
  • switch branch
git checkout 分支名
//	创建并切换
git checkout -b 分支名
  • delete branch
git branch -d 分支名
  • Merge branches, assuming that the current branch name is 1, execute the following command to indicate: Merge the code of branch name 2 into branch name 1, it is just code merging, if there is a code conflict, it needs to be resolved manually
git merge 分支名2
  • Rebase operation (generally not used by multi-person collaboration), move the modified content of the current branch to the specified branch. Then specify that the branch needs to be merged once (with the current branch)
git rebase 分支名
//	指定分支变基
git rebase  目标分支 指定分支

//	当指定分支不是从目标分支直接分出来的时候 
//	假设 基于 分支A 创建了 分支B
//	基于 分支B 创建了 分支C
//	变基操作命令如下:
git rebase --onto 分支A 分支B 分支C

code management

  • View file status
git status

git status -s
git status -short
  • Code local git temporary storage
git add 文件名
//	全部暂存(.gitignore文件中的范围文件将忽略)
git add .
  • Remove files from staging area
git rm --cached 文件名
  • Submit code locally (local warehouse)
//	提交暂存的内容
git commit -m "此次提交描述信息"

//	直接提交修改的内容
git commit -m -a "此次提交描述信息"
  • Pull remote branch data
git pull origin(远程仓库地址) 远程分支名(默认master)
  • Push data to the remote branch
git push origin(远程仓库地址) -u 远程分支名

//强制推送
git push -f origin(远程仓库地址) -u 远程分支名
  • View local commit information

git log 

press q to quit
insert image description here

Code History Commit Actions

  • To reset to a commit state in the history, operations 1 and 2 are required. If you create a new branch based on the current branch in advance (recommended), you do not need to perform 3 and 4.
//1. 查看log日志, 找到目标提交id
git log --pretty=oneline

//2. 重置到的目标提交状态
git reset --hard 目标提交id

//3. 查看引用日志, 最左侧为id
//	找到最新的提交id,一般是引用HEAD@{
    
    1}的id,因为你刚才一次reset
git relog
//4. 新建分支指定最新的提交id
git branch new分支名 最新的提交id

epilogue

详细信息请自行搜索git官网查看。

Guess you like

Origin blog.csdn.net/qq_43231248/article/details/129085701
Recommended