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

Posted 痞子衡嵌入式

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了痞子衡嵌入式:第一本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()函数
[email protected] 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()函数的改动缓存起来
[email protected] 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空间很干净,说明缓存成功
[email protected] 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()函数,并依次缓存
[email protected] MINGW64 /d/my_project/gittest (master)
$ git stash save -a "add test_stash1()"

Saved working directory and index state On master: add test_stash1()

[email protected] 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,此时显示共有三次缓存
[email protected] MINGW64 /d/my_project/gittest (master)
$ git stash list

[email protected]{0}: On master: add test_stash2()
[email protected]{1}: On master: add test_stash1()
[email protected]{2}: On master: add test_stash0()

1.3查看某个已缓存改动的具体细节git stash show -p [[email protected]{n}]

// 查看编号为 [email protected]{1} 的缓存的具体改动
[email protected] MINGW64 /d/my_project/gittest (master)
$ git stash show -p [email protected]{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 [[email protected]{n}]

  现在我们需要从缓存区恢复某个已缓存改动,可以直接用git stash pop恢复最近的一次缓存,也可以用git stash pop [email protected]{n} 恢复任意指定的一次缓存(也可以用git stash pop apply [email protected]{n} 来恢复某个缓存,但是apply命令并不会将被恢复的缓存改动从缓存区list里删除)

// 将编号为 [email protected]{1} 的缓存恢复
[email protected] MINGW64 /d/my_project/gittest (master)
$ git stash pop [email protected]{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 [email protected]{1} (62daecdc826586bb3c0cbe93c5f8d2e2697e9ea)

// 查看原编号为 [email protected]{1} 的缓存的具体改动,确实已正常恢复
[email protected] 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()"(原编号 [email protected]{1} 已被释放)已不在
[email protected] MINGW64 /d/my_project/gittest (master)
$ git stash list

[email protected]{0}: On master: add test_stash2()
[email protected]{1}: On master: add test_stash0()

1.5丢弃某个已缓存改动git stash drop [[email protected]{n}]

// 从缓存list里直接删除编号为 [email protected]{1} 的缓存
[email protected] MINGW64 /d/my_project/gittest (master)
$ git stash drop [email protected]{1}

Dropped [email protected]{1} (2f5dd9a45f77bcb24cac247b8f88bdec157798f2)

// 查看缓存list里被删除的缓存"add test_stash0()"(原编号 [email protected]{1} 已被释放)已不在
[email protected] MINGW64 /d/my_project/gittest (master)
$ git stash list

[email protected]{0}: On master: add test_stash2()

1.6清空所有已缓存改动git stash clear

// 清空缓存list
[email protected] MINGW64 /d/my_project/gittest (master)
$ git stash clear

// 查看缓存list,其已被清空
[email protected] MINGW64 /d/my_project/gittest (master)
$ git stash list

以上是关于痞子衡嵌入式:第一本Git命令教程(7.1)- 清理之缓存(stash)的主要内容,如果未能解决你的问题,请参考以下文章

痞子衡嵌入式:第一本Git命令教程- 编辑(status/add/rm/mv)

痞子衡嵌入式:第一本Git命令教程- 提交(commit/format-patch/am)

痞子衡嵌入式:极精简的Git命令教程- 连接(remote/clone)

痞子衡嵌入式:极精简的Git命令教程- 准备(init/config/.gitignore)

《痞子衡嵌入式半月刊》 第 75 期

《痞子衡嵌入式半月刊》 第 2 期