今天是 Git 系列課程第七課,上一課我們學(xué)會(huì)了查看 Git 本地歷史提交,今天痞子衡要講的是 Git 倉庫的清理操作,一共 4 個(gè)命令,都是日常開發(fā)中非常實(shí)用的命令,掌握這 4 個(gè)命令,會(huì)讓你有一種玩弄 Git 倉庫于股掌的感覺。
由于本節(jié)課是教程的核心課程,所以會(huì)分 4 小節(jié)課來講,第一講介紹 git stash
1. 緩存文件改動(dòng) git stash
試想一下你在使用 Git 時(shí)有沒有這樣的經(jīng)歷,你正在寫代碼(修改文件),但是代碼還沒有寫完善,沒達(dá)到提交的標(biāo)準(zhǔn),但是你知道了有另一個(gè) team member 推送了一個(gè)提交,這個(gè)提交你需要立刻同步到你的本地,此時(shí)怎么辦?是的,你需要本地緩存你的改動(dòng)。
1.1 緩存當(dāng)前改動(dòng) git stash [save -a "description"]
// 在 test.c 文件里增加一個(gè) 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 testn"); // 將增加 test_stash0()函數(shù)的改動(dòng)緩存起來 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 查看所有已緩存改動(dòng)列表 git stash list
// 查看緩存 list,此時(shí)顯示共有三次緩存 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 查看某個(gè)已緩存改動(dòng)的具體細(xì)節(jié) git stash show -p [stash@{n}]
// 查看編號(hào)為 stash@{1} 的緩存的具體改動(dòng) 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 testn");
1.4 恢復(fù)某個(gè)已緩存改動(dòng) git stash pop [stash@{n}]
現(xiàn)在我們需要從緩存區(qū)恢復(fù)某個(gè)已緩存改動(dòng),可以直接用 git stash pop 恢復(fù)最近的一次緩存,也可以用 git stash pop stash@{n} 恢復(fù)任意指定的一次緩存(也可以用 git stash pop apply stash@{n} 來恢復(fù)某個(gè)緩存,但是 apply 命令并不會(huì)將被恢復(fù)的緩存改動(dòng)從緩存區(qū) list 里刪除)
// 將編號(hào)為 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) // 查看原編號(hào)為 stash@{1} 的緩存的具體改動(dòng),確實(shí)已正?;謴?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 testn"); // 查看緩存 list 里被恢復(fù)的緩存"add test_stash1()"(原編號(hào) 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 丟棄某個(gè)已緩存改動(dòng) git stash drop [stash@{n}]
// 從緩存 list 里直接刪除編號(hào)為 stash@{1} 的緩存 jay@pc MINGW64 /d/my_project/gittest (master)$ git stash drop stash@{1}
Dropped stash@{1} (2f5dd9a45f77bcb24cac247b8f88bdec157798f2)
// 查看緩存 list 里被刪除的緩存"add test_stash0()"(原編號(hào) stash@{1} 已被釋放)已不在 jay@pc MINGW64 /d/my_project/gittest (master)$ git stash list
stash@{0}: On master: add test_stash2()
1.6 清空所有已緩存改動(dòng) 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