git常用命令总结

作为一名程序员,git 的使用是必须的,本篇就将作者自己工作中常用的命令进行说明。

拉取对应分支

bash 复制代码
mkdir myNotes        	 	# 创建目录
cd myNotes                	# 进入目录
git init                    # 初始化GIT
git remote add origin https://gitee.com/aSimpleSheep/myNotes.git      # 配置远程仓库
# 或者直接 clone
git clone https://gitee.com/aSimpleSheep/myNotes.git
git config --global http.sslVerify false               				  # 关闭ssl验证
git config --global user.name "Firstname Lastname"     				  # 配置GIT用户名
git config --global credential.helper store      					  # 记住密码
git fetch    														  # 将远程分支信息获取到本地
git pull origin master												  # 获取远程master分支
git checkout -b local_master origin/master							  # 新建本地分支local_master,然后从远程分支master拉取代码到新分支local_master

生成 patch

git diff > xxx.patch

bash 复制代码
# 只想 patch Test.c 文件
git diff Test.c > test.patch
# 把所有的修改文件打成 patch
git diff > test.patch

git format-patch

bash 复制代码
git format-patch HEAD^       # 生成最近的1次commit的patch
git format-patch HEAD^^      # 生成最近的2次commit的patch
git format-patch HEAD^^^     # 生成最近的3次commit的patch
git format-patch HEAD^^^^    # 生成最近的4次commit的patch
git format-patch <r1>..<r2>  # 生成两个commit间的修改的patch(生成的patch不包含r1. <r1>和<r2>都是具体的commit号)
git format-patch -1 <r1>     # 生成单个commit的patch
git format-patch <r1>        # 生成某commit以来的修改patch(不包含该commit)
git format-patch --root <r1> # 生成从根到r1提交的所有patch

应用 patch

检查 patch 的情况

bash 复制代码
git apply --stat 0001-limit-log-function.patch		# 查看patch的情况
git apply --check 0001-limit-log-function.patch		# 检查patch是否能够打上,如果没有任何输出,则说明无冲突,可以打上

打 patch 场景之一使用 git diff 生成的 patch

bash 复制代码
git apply xxx.patch

打 patch 场景之二使用 git format-patch 生成的 patch

bash 复制代码
git am 0001-limit-log-function.patch           # 将名字为0001-limit-log-function.patch的patch打上
git am --signoff 0001-limit-log-function.patch # 添加-s或者--signoff,还可以把自己的名字添加为signed off by信息,作用是注明打patch的人是谁,因为有时打patch的人并不是patch的作者
git am ~/patch-set/*.patch                     # 将路径~/patch-set/*.patch 按照先后顺序打上
git am --abort                                 # 当git am失败时,用以将已经在am过程中打上的patch废弃掉(比如有三个patch,打到第三个patch时有冲突,那么这条命令会把打上的前两个patch丢弃掉,返回没有打patch的状态)
git am --resolved                              # 当git am失败,解决完冲突后,这条命令会接着打patch

补充

工作中还会经常遇到 2 种场景:

  1. 自己 commit 提交次数太多,导致 merge 失败。此时就需要合并多次 commit
bash 复制代码
	git rebase -i HEAD^3		# 合并最近3次 commit 提交
  1. 代码移植:移植相关功能 commit 到其他项目分支
bash 复制代码
	git cherry-pick <commit id>						# 移植对应 commit 到当前分支
	git cherry-pick <commit id1> <commit id2>		# 移植多个 commit 到当前分支
	git cherry-pick <commit id1>..<commit id2>		# 移植 commit1 ~ commit2 之间的所有到当前分支
相关推荐
会飞的青蛙13 分钟前
GIT 配置别名&脚本自动化执行
前端·git
使一颗心免于哀伤1 小时前
《设计模式之禅》笔记摘录 - 21.状态模式
笔记·设计模式
权咚2 天前
阿权的开发经验小集
git·ios·xcode
_落纸2 天前
三大基础无源电子元件——电阻(R)、电感(L)、电容(C)
笔记
Alice-YUE2 天前
【CSS学习笔记3】css特性
前端·css·笔记·html
2303_Alpha2 天前
SpringBoot
笔记·学习
Hello_Embed2 天前
STM32HAL 快速入门(二十):UART 中断改进 —— 环形缓冲区解决数据丢失
笔记·stm32·单片机·学习·嵌入式软件
咸甜适中2 天前
rust语言 (1.88) 学习笔记:客户端和服务器端同在一个项目中
笔记·学习·rust
Grassto2 天前
RAG 从入门到放弃?丐版 demo 实战笔记(go+python)
笔记
Magnetic_h2 天前
【iOS】设计模式复习
笔记·学习·ios·设计模式·objective-c·cocoa