今天是 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.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()函数的改动缓存起来 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()函数,并依次缓存 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.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 恢复某个已缓存改动 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 ..." 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} 的缓存的具体改动,确实已正常恢复 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 里被恢复的缓存"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