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 之间的所有到当前分支
相关推荐
demodeom1 小时前
Win11 + RTX 5080 本地 Coding 模型测试笔记(已弃坑)
笔记
10mAh1 小时前
【Git】误删分支、reset --hard 后提交丢了怎么办?——reflog、fsck 与安全恢复实战
数据库·git·安全·回归
peijiping2 小时前
Agent 工具执行:并行(parallel_tool_calls)和后台(run_in_background)到底有什么区别? (学习笔记)
笔记·学习·ai agent·claude code
Oll Correct2 小时前
Adobe illustrator 案例四:吃豆人 (Pac-Man)图标绘制
笔记·ui·adobe·illustrator
LuminousCPP2 小时前
单链表专题(四)-刷题复盘篇-LeetCode 138 随机链表复制|原地拷贝法突破复杂指针操作
数据结构·笔记·算法·leetcode·链表
萌新小白YXY2 小时前
SAE J1587/SAE J1708 协议笔记
笔记·单片机·嵌入式硬件
IT古董3 小时前
【MES学习笔记系列】MES 推荐书籍与资源
笔记·学习
一米阳光86613 小时前
软考(中级)软件设计师核心笔记(9)算法——时间复杂度与空间复杂度、查找算法、排序算法
笔记·算法·职场发展·软考·软件设计师·中级职称
Lydro3 小时前
CentOS 7 安装高版本git
git·centos 7
米饭不加菜3 小时前
常用基本求导公式与法则(超详细推导版——上)
经验分享·笔记