第一本Git命令教程(7.1)-清理之缓存

今天是 git 系列课程第七课,上一课我们学会了查看 git 本地历史提交,今天痞子衡要讲的是 git 仓库的清理操作,一共 4 个命令,都是日常开发中非常实用的命令,掌握这 4 个命令,会让你有一种玩弄 git 仓库于股掌的感觉。
由于本节课是教程的核心课程,所以会分 4 小节课来讲,第一讲介绍 git stash
1. 缓存文件改动 git stash
试想一下你在使用 git 时有没有这样的经历,你正在写代码(修改文件),但是代码还没有写完善,没达到提交的标准,但是你知道了有另一个 team member 推送了一个提交,这个提交你需要立刻同步到你的本地,此时怎么办?是的,你需要本地缓存你的改动。
1.1 缓存当前改动 git stash [save -a description]
// 在 test.c 文件里增加一个 test_stash0()函数 jay@pc mingw64 /d/my_project/gittest (master)$ git diff app/test.c
diff --git a/app/test.c b/app/test.cindex 70dde01..38b763c 100644--- a/app/test.c+++ b/app/test.c@@ -1,5 +1,8 @@ #include #include +void test_stash0(void)+{+} void test(void) { printf(this is test/n);  
// 将增加 test_stash0()函数的改动缓存起来 jay@pc mingw64 /d/my_project/gittest (master)$ git stash save -a add test_stash0()
saved working directory and index state on master: add test_stash0()  
// 缓存之后查看 git 空间很干净,说明缓存成功 jay@pc mingw64 /d/my_project/gittest (master)$ git status
on branch masteryour branch is ahead of 'origin/master' by 2 commits. (use git push to publish your local commits)nothing to commit, working tree clean  
// 在 test.c 文件里再依次 test_stash1()、test_stash2()函数,并依次缓存 jay@pc mingw64 /d/my_project/gittest (master)$ git stash save -a add test_stash1()
saved working directory and index state on master: add test_stash1()  
jay@pc mingw64 /d/my_project/gittest (master)$ git stash save -a add test_stash2()
saved working directory and index state on master: add test_stash2()  
1.2 查看所有已缓存改动列表 git stash list
// 查看缓存 list,此时显示共有三次缓存 jay@pc mingw64 /d/my_project/gittest (master)$ git stash list
stash@{0}: on master: add test_stash2()stash@{1}: on master: add test_stash1()stash@{2}: on master: add test_stash0()  
1.3 查看某个已缓存改动的具体细节 git stash show -p [stash@{n}]
// 查看编号为 stash@{1} 的缓存的具体改动 jay@pc mingw64 /d/my_project/gittest (master)$ git stash show -p stash@{1}
diff --git a/app/test.c b/app/test.cindex 70dde01..4380571 100644--- a/app/test.c+++ b/app/test.c@@ -1,5 +1,8 @@ #include #include +void test_stash1(void)+{+} void test(void) { printf(this is test/n);  
1.4 恢复某个已缓存改动 git stash pop [stash@{n}]
现在我们需要从缓存区恢复某个已缓存改动,可以直接用 git stash pop 恢复最近的一次缓存,也可以用 git stash pop stash@{n} 恢复任意指定的一次缓存(也可以用 git stash pop apply stash@{n} 来恢复某个缓存,但是 apply 命令并不会将被恢复的缓存改动从缓存区 list 里删除)
// 将编号为 stash@{1} 的缓存恢复 jay@pc mingw64 /d/my_project/gittest (master)$ git stash pop stash@{1}
on branch masteryour branch is ahead of 'origin/master' by 2 commits. (use git push to publish your local commits)changes not staged for commit: (use git add ... to update what will be committed) (use git checkout -- ... to discard changes in working directory) modified: app/test.cno changes added to commit (use git add and/or git commit -a)dropped stash@{1} (62daecdc826586bb3c0cbe93c5f8d2e2697e9ea)  
// 查看原编号为 stash@{1} 的缓存的具体改动,确实已正常恢复 jay@pc mingw64 /d/my_project/gittest (master)$ git diff app/test.c
diff --git a/app/test.c b/app/test.cindex 70dde01..38b763c 100644--- a/app/test.c+++ b/app/test.c@@ -1,5 +1,8 @@ #include #include +void test_stash0(void)+{+} void test(void) { printf(this is test/n);  
// 查看缓存 list 里被恢复的缓存add test_stash1()(原编号 stash@{1} 已被释放)已不在 jay@pc mingw64 /d/my_project/gittest (master)$ git stash list
stash@{0}: on master: add test_stash2()stash@{1}: on master: add test_stash0()  
1.5 丢弃某个已缓存改动 git stash drop [stash@{n}]
// 从缓存 list 里直接删除编号为 stash@{1} 的缓存 jay@pc mingw64 /d/my_project/gittest (master)$ git stash drop stash@{1}
dropped stash@{1} (2f5dd9a45f77bcb24cac247b8f88bdec157798f2)  
// 查看缓存 list 里被删除的缓存add test_stash0()(原编号 stash@{1} 已被释放)已不在 jay@pc mingw64 /d/my_project/gittest (master)$ git stash list
stash@{0}: on master: add test_stash2()  
1.6 清空所有已缓存改动 git stash clear
// 清空缓存 list jay@pc mingw64 /d/my_project/gittest (master)$ git stash clear
// 查看缓存 list,其已被清空 jay@pc mingw64 /d/my_project/gittest (master)$ git stash list


安森美完成收购GT Advanced Technologies
基于RSSI_2M门限的BLE速率自适应算法流程
微软正积极为 Android 发展做贡献
BB2022L是一款基于X波段雷达芯片而设计的微/运动感知模组
MIT实现人造肌肉纤维,仿生机器人成可能
第一本Git命令教程(7.1)-清理之缓存
触摸屏与多台PLC之间无线Ethernet通信
6DNS系统将全面提升我国高校IPv6上网体验
《Nature》发布毫米级软体机器人,为人类研究体内微型机器人提供新思路
MIT博士用空间噪声滤波法实现超灵敏量子传感器
HTC Vive Focus VR一体机开启预购,明年1月发售
美格智能Cat.1蜂窝IPC解决方案 助力安防监控智慧连接
高清音频正在改变着我们的收听方式
作为汽车界的小米 奇点汽车未来前途迷茫
分布式账本联盟R3即将发布一项基于Corda技术的区块链跨境支付平台
光纤智能床垫解决老人护理难
百度发布了《了不起的中国AI》系列视频
MR Studio将推出Revit插件 打造全功能MR或AR体验
ESG成全球风潮,联想造了一个可持续的“进托邦”
TE Connectivity与Heilind携手参与西部电子论坛 助力西部产业升级