git stash用法总结

git stash命令的作用就是将目前还不想提交的但是已经修改的内容进行保存,后续可以在任何分支上进行恢复。
git stash命令的作用范围包括工作区和暂存区中的内容,也就是说git add但没有git commit的内容和没有git add的内容都会被保存,所有的内容会保存在内存中,每次保存的内容会以stash@{index}为头标题的方式显示。

最新的保存内容永远为stash@{0}

git stash的常用命令:

1、git stash :保存所有未提交的修改。

PS D:\workspace\web> git stash
Saved working directory and index state WIP on test: 38680ae Merge branch 'test' into test
PS D:\workspace\web> git stash list
stash@{0}: WIP on test: 38680ae Merge branch 'test' into test

2、git stash save [message] :保存所有未提交的修改,并添加注释信息。(推荐使用方式

PS D:\workspace\web> git stash save "router and totalRowTable"

3、git stash list :查看内存中所有保存的stash。

PS D:\workspace\web> git stash list
stash@{0}: On feature-v1.2.2: router and totalRowTable  //最新一次的保存
stash@{1}: On feature-v1.2.2: totalAmountRow1

4、git stash apply:将stash@{0}应用到当前目录下,内存中stash不做任何修改。
5、git stash apply "stash@{1}" || git stash apply --index 1:将stash@{1}的内容应用到当前目录下,内存中stash不做任何修改。

PS D:\workspace\web> git stash apply "stash@{1}"
Auto-merging src/pages/Voucher/FinanceDetail/components/ListForm.jsx
On branch test
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   config/router.config.js
        new file:   src/components/TotalRowTable/index.jsx
        modified:   src/locales/zh-CN/menu.js
        new file:   src/pages/TaskMgr/BatchRecords/index.jsx
        new file:   src/pages/TaskMgr/TaskManager/index.jsx
        new file:   src/pages/Voucher/AccountApproval/index.jsx
        new file:   src/pages/Voucher/AccountRecords/index.jsx
        modified:   src/pages/Voucher/FinanceDetail/components/ListForm.jsx
        modified:   src/pages/Voucher/FinanceDetail/components/ListTable.jsx
        modified:   src/pages/Voucher/FinanceDetail/index.jsx
        new file:   src/pages/Voucher/models/getFinanceList.js

PS D:\workspace\web>

6、git stash pop
将stash@{0}中的内容应用到当前分支对应的工作目录上,并且在内存中删除stash@{0}。
注:该命令将以栈的执行方式删除,(栈:先进后出,后进先出)。

PS D:\workspace\web> git stash list  //总共有2条stash
stash@{1}: On feature-v1.2.2: test1
stash@{2}: On feature-v1.2.2: test2
PS D:\workspace\web> git stash pop  //执行pop命令
On branch test
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   config/router.config.js
        new file:   src/components/TotalRowTable/index.jsx
        modified:   src/locales/zh-CN/menu.js
        new file:   src/pages/TaskMgr/BatchRecords/index.jsx
        new file:   src/pages/TaskMgr/TaskManager/index.jsx
        new file:   src/pages/Voucher/AccountApproval/index.jsx
        new file:   src/pages/Voucher/AccountRecords/index.jsx
        modified:   src/pages/Voucher/FinanceDetail/components/ListForm.jsx
        modified:   src/pages/Voucher/FinanceDetail/components/ListTable.jsx
        modified:   src/pages/Voucher/FinanceDetail/index.jsx
        new file:   src/pages/Voucher/models/getFinanceList.js

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:   config/config.js

Dropped refs/stash@{0} (cfdd1ea72d1428cbf0d97866d9abb26d9cb83c25)
PS D:\workspace\web> git stash list  //再次查询stash,只有1条stash
stash@{0}: On feature-v1.2.2: test2
PS D:\workspace\orafl-financing-web>

7、git stash pop "stash@{1}" || git stash pop --index 1:将内存中的stash@{1}应用到当前目录,并在内存中删除当前stash。
8、git stash show:查看stash@{0}和当前目录的差异。

PS D:\workspace\web> git stash show
 config/router.config.js                            |  66 ++++++
 src/components/TotalRowTable/index.jsx             |  61 ++++++
 src/locales/zh-CN/menu.js                          |   8 +
 src/pages/TaskMgr/BatchRecords/index.jsx           |   0
 src/pages/TaskMgr/TaskManager/index.jsx            |   0
 src/pages/Voucher/AccountApproval/index.jsx        |   0
 src/pages/Voucher/AccountRecords/index.jsx         |   0
 .../Voucher/FinanceDetail/components/ListForm.jsx  |  20 +-
 .../Voucher/FinanceDetail/components/ListTable.jsx | 225 +++++++++++++++++----
 src/pages/Voucher/FinanceDetail/index.jsx          |  13 +-
 src/pages/Voucher/models/getFinanceList.js         |  34 ++++
 11 files changed, 367 insertions(+), 60 deletions(-)
PS D:\workspace\web>

9、git stash show -p:查看最新保存的stash和当前目录的差异详情。

PS D:\workspace\web> git stash show -p
diff --git a/config/router.config.js b/config/router.config.js
index a47aa25..d9fe82d 100644
--- a/config/router.config.js
+++ b/config/router.config.js
@@ -128,8 +128,74 @@ export default [
               }
             ],
           },
+          // 凭证明细调帐审批
+          {
+            path: '/voucher/accountApproval',
+            name: 'accountApproval',
+            routes: [
+              {
+                path: '/voucher/accountApproval',
+                redirect: '/voucher/accountApproval/list',
+              },
+              {
+                path: '/voucher/accountApproval/list',
+                component: './Voucher/AccountApproval',
+              }
+            ],
+          },
:

10、git stash clear :清除内存中所有的stash
11、git stash drop :从内存中移除stash@{0}
12、git stash drop "stash@{1} | "git stash drop -q 1 :从内存中移除索引为1的stash
13、git stash branch test "stash@{1}" :新建test分支并切换到test分支,并将stash@{1}的内容应用到test分支。

git stash的详细命令用法:

usage: git stash list [<options>]
   or: git stash show [<options>] [<stash>]
   or: git stash drop [-q|--quiet] [<stash>]
   or: git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
   or: git stash branch <branchname> [<stash>]
   or: git stash clear
   or: git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
          [-u|--include-untracked] [-a|--all] [-m|--message <message>]
          [--] [<pathspec>...]]
   or: git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
          [-u|--include-untracked] [-a|--all] [<message>]

PS D:\workspace\web>

查看更详细的说明,可以在控制台输出命令:git stash --help

参考:git stash详解

发布了45 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/ThisEqualThis/article/details/103362644