回到列表

git stash

Intro

暂存本地仓库中的改动

常用命令说明(以purchase-view为例)

# git help stash中的内容
git stash list [<options>]
git stash show [<options>] [<stash>]
git stash drop [-q|--quiet] [<stash>]
git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
git stash branch <branchname> [<stash>]
git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
              [-u|--include-untracked] [-a|--all] [-m|--message <message>]
              [--] [<pathspec>...]]
git stash clear
git stash create [<message>]
git stash store [-m|--message <message>] [-q|--quiet] <commit>

stash push

将变更内容暂存入堆栈

# stash前先修改本地文件
➜  purchase-view git:(test) echo 'add something' >> README.md
➜  purchase-view git:(test)echo '// add something2' >> babel.config.js
  1. stash所有改动(push可省略)
➜  purchase-view git:(test)git stash
  1. stash指定文件(push不可省略)
➜  purchase-view git:(test)git stash push README.md
➜  purchase-view git:(test)git stash push babel.config.js
  1. stash unstaged的文件(—keep-index/-k)
# "README.md"已经add
➜  purchase-view git:(test)git status
On branch test
Your branch is up to date with 'origin/test'.
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   README.md
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   babel.config.js

➜  purchase-view git:(test)git stash push --keep-index
Saved working directory and index state WIP on test: e82c8f29 Merge branch 'feature/ISC-8693' into 'test'

➜  purchase-view git:(test)git status
On branch test
Your branch is up to date with 'origin/test'.
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   README.md
  1. 同理,还有[-u|—include-untracked] [-a|—all]参数可以控制stash push的内容

stash list

展示stash中的堆栈内容

# 承接stash push中"stash指定文件"
➜  purchase-view git:(test) git stash list
# 结果展示如下
stash@{0}: WIP on test: e82c8f29 Merge branch 'feature/ISC-8693' into 'test'
stash@{1}: WIP on test: e82c8f29 Merge branch 'feature/ISC-8693' into 'test'

stash show

展示stash中某条记录信息

# 承接stash push中"stash指定文件"
➜  purchase-view git:(test) git stash show stash@\{0\}
 babel.config.js | 1 +
 1 file changed, 1 insertion(+)
# 支持传入git diff的参数修饰,例如: "-p" ==> 展示文件内容

➜  purchase-view git:(test) git stash show stash@\{0\} -p
──────────────────────────────────────────────────────────────────────────────────────────────────────────────
modified: babel.config.js
──────────────────────────────────────────────────────────────────────────────────────────────────────────────
@ babel.config.js:53 @ module.exports = {
    ]
  ]
};
// add something2

stash drop

删除stash堆栈中指定的某次记录。如果没有指定记录,则移除栈顶的一条。

# 承接stash push中"stash指定文件"
➜  purchase-view git:(test) git stash drop stash@\{1\}

# drop后查看当前的堆栈,只剩一条记录
➜  purchase-view git:(test) git stash list
stash@{0}: WIP on test: e82c8f29 Merge branch 'feature/ISC-8693' into 'test'

stash clear

清空stash堆栈。⚠️不可找回,操作前须谨慎。

stash pop

将stash堆栈中指定的某次记录应用到当前代码库并删除。如果没有指定记录,则使用栈顶的一条。

# 承接stash push中"stash指定文件"
➜  purchase-view git:(test) git stash pop
On branch test
Your branch is up to date with 'origin/test'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   babel.config.js
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (16f64a42190a46c8d11541313f9a5b6d4ac1c9ef)

➜  purchase-view git:(test)git stash list
stash@{0}: WIP on test: e82c8f29 Merge branch 'feature/ISC-8693' into 'test'

stash apply

对当前仓库的操作同pop,但不删除stash堆栈的记录。

# 承接stash push中"stash指定文件"
➜  purchase-view git:(test) git stash apply
On branch test
Your branch is up to date with 'origin/test'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   babel.config.js
no changes added to commit (use "git add" and/or "git commit -a")

➜  purchase-view git:(test)git stash list
stash@{0}: WIP on test: e82c8f29 Merge branch 'feature/ISC-8693' into 'test'
stash@{1}: WIP on test: e82c8f29 Merge branch 'feature/ISC-8693' into 'test'

E.G

• 本地仓库存在修改的时候需要更新远端仓库代码;

• 本地仓库存在修改的时候需要切换仓库;

• 本地代码分多次提交测试。