今天是 Git 系列課程第七課,上一課我們學(xué)會了查看 Git 本地歷史提交,今天痞子衡要講的是 Git 倉庫的清理操作,一共 4 個命令,都是日常開發(fā)中非常實用的命令,掌握這 4 個命令,會讓你有一種玩弄 Git 倉庫于股掌的感覺。
由于本節(jié)課是教程的核心課程,所以會分 4 小節(jié)課來講,第一講介紹 git stash
1. 緩存文件改動 git stash
試想一下你在使用 Git 時有沒有這樣的經(jīng)歷,你正在寫代碼(修改文件),但是代碼還沒有寫完善,沒達到提交的標(biāo)準(zhǔn),但是你知道了有另一個 team member 推送了一個提交,這個提交你需要立刻同步到你的本地,此時怎么辦?是的,你需要本地緩存你的改動。
1.1 緩存當(dāng)前改動 git stash [save -a "description"]
// 在 test.c 文件里增加一個 test_stash0()函數(shù) jay@pc MINGW64 /d/my_project/gittest (master)$ git diff app/test.c
diff --git a/app/test.c b/app/test.c
index 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()函數(shù)的改動緩存起來 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 master Your 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()函數(shù),并依次緩存 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 查看某個已緩存改動的具體細(xì)節(jié) 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.c
index 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 恢復(fù)某個已緩存改動 git stash pop [stash@{n}]
現(xiàn)在我們需要從緩存區(qū)恢復(fù)某個已緩存改動,可以直接用 git stash pop 恢復(fù)最近的一次緩存,也可以用 git stash pop stash@{n} 恢復(fù)任意指定的一次緩存(也可以用 git stash pop apply stash@{n} 來恢復(fù)某個緩存,但是 apply 命令并不會將被恢復(fù)的緩存改動從緩存區(qū) list 里刪除)
// 將編號為 stash@{1} 的緩存恢復(fù) jay@pc MINGW64 /d/my_project/gittest (master)$ git stash pop stash@{1}
On branch master
Your 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.c
no changes added to commit (use "git add" and/or "git commit -a")
Dropped stash@{1} (62daecdc826586bb3c0cbe93c5f8d2e2697e9ea)
// 查看原編號為 stash@{1} 的緩存的具體改動,確實已正?;謴?fù) jay@pc MINGW64 /d/my_project/gittest (master)$ git diff app/test.c
diff --git a/app/test.c b/app/test.c
index 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 里被恢復(fù)的緩存"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
審核編輯黃昊宇
-
Git
+關(guān)注
關(guān)注
0文章
207瀏覽量
16930
發(fā)布評論請先 登錄
深度解析LMK04616:超低噪聲時鐘抖動清理器的卓越之選
海光整機搭配銀河麒麟桌面操作系統(tǒng)V10SP1(X86)「使用命令掛載磁盤報錯結(jié)構(gòu)需要清理」問題解決方法
ESP32 編譯過程中 bootloader 配置階段的 CMake 緩存沖突錯誤,記錄
緩存之美:萬文詳解 Caffeine 實現(xiàn)原理(上)
harmony-utils之LRUCacheUtil,LRUCache緩存工具類
高性能緩存設(shè)計:如何解決緩存偽共享問題
MCU緩存設(shè)計
Nginx緩存配置詳解
在KiCad的PCB編輯其中,有一個實用的工具,可以用來清理布線與過孔
高速SSD存儲系統(tǒng)中數(shù)據(jù)緩存控制器整體頂層設(shè)計
第一本Git命令教程(7.1)-清理之緩存
評論