痞子衡嵌入式:第一本Git命令教程(7.1)- 清理之缓存(stash)


  今天是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 <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 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 <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.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 <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 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

欢迎订阅

文章会同时发布到我的 博客园主页CSDN主页微信公众号 平台上。

微信搜索"痞子衡嵌入式"或者扫描下面二维码,就可以在手机上第一时间看了哦。

posted @ 2016-12-25 21:53  痞子衡  阅读(666)  评论(0编辑  收藏  举报