今天是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.cdiff --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 <stdio.h> #include <stdlib.h> +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 statusOn 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()函式,並依次快取
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 liststash@{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.c index 70dde01..4380571 100644 --- a/app/test.c +++ b/app/test.c @@ -1,5 +1,8 @@ #include <stdio.h> #include <stdlib.h> +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 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 <file>..." to update what will be committed) (use "git checkout -- <file>..." 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} 的快取的具體改動,確實已正常恢復
jay@pc MINGW64 /d/my_project/gittest (master)
$ git diff app/test.cdiff --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 <stdio.h> #include <stdlib.h> +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 liststash@{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 liststash@{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