当前位置:   article > 正文

Git常用命令stash_git stash 命令大全

git stash 命令大全

Git常用命令stash

封存改动:将一个修改后的工作区中的改动保存起来,将工作区恢复到改动前的状态。

echo a > a.txt
git add a.txt
git commit -m "add a.txt"

echo b > b.txt
git add b.txt
git commit -m "add b.txt"

echo c > c.txt
git add c.txt
git commit -m "add c.txt"

$ git log --oneline
3fc4490 (HEAD -> master) add c.txt
36208e4 add b.txt
bfd3430 add a.txt

$ echo a1 > a.txt

$ git status
On branch master
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:   a.txt

no changes added to commit (use "git add" and/or "git commit -a")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
# 该命令将临时保存所有修改的文件
# 将所有未提交的修改(提交到暂存区)保存至堆栈中
$ git stash
# 例子
$ git stash
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory.
Saved working directory and index state WIP on master: 3fc4490 add c.txt
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
# 给本次存储加个备注,以防时间久了忘了
$ git stash save "描述信息"
# 例子
$ git stash save "update a.txt"
Saved working directory and index state On master: update a.txt

$ git status
On branch master
nothing to commit, working tree clean
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
# 弹出第一个被stash的记录,并且该stash会从历史记录中删除
# 等价于git stash apply + git stash drop
$ git stash pop
On branch master
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:   a.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (fd7b7dd9bb42def1a429bb3f54b27fa0d31f1247)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
# 该命令将显示stash的所有变更
$ git stash list
stash@{0}: On master: update a.txt
  • 1
  • 2
  • 3
# 存储未追踪的文件,也就是没有git add的文件 
$ echo d > d.txt

$ git status
On branch master
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:   a.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        d.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git stash
Saved working directory and index state WIP on master: 3fc4490 add c.txt

# git stash不会将为未进行git add的文件进行暂存
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        d.txt

nothing added to commit but untracked files present (use "git add" to track)

zhangshixing@DESKTOP-CR3IL33 MINGW64 ~/Desktop/test/h1 (master)
$ git stash pop
On branch master
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:   a.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        d.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (ea55abf2e9fe8ce2bda5509c1b3d1152910432e6)

# 会将未进行git add的文件进行暂存
$ git stash -u
Saved working directory and index state WIP on master: 3fc4490 add c.txt

$ git status
On branch master
nothing to commit, working tree clean

$ git stash pop
On branch master
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:   a.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        d.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (1b2fd1d1639bbd68235201221ee734d392536a9c)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
# 该命令将丢弃最近一次stash的变更
$ git stash drop
  • 1
  • 2
# 弹出stash,但是这条命令stash仍然会保存在stash历史记录中
$ git stash apply
  • 1
  • 2
# 在Windows上和PowerShell中,需要加双引号
# 恢复后stash记录并不删除
$ git stash apply "stash@{index}"
# 应用第一次暂存
$ git stash apply stash@{0}
  • 1
  • 2
  • 3
  • 4
  • 5
# 恢复的同时把stash记录也删了
$ git stash pop "stash@{index}"
  • 1
  • 2
# 删除stash记录
$ git stash drop "stash@{index}"
  • 1
  • 2
# 查看当前记录中修改了哪些文件
$ git stash show "stash@{index}"
  • 1
  • 2
# 查看当前记录中修改了哪些文件的内容
$ git stash show -p "stash@{index}"
# 参考第一次暂存
$ git stash show -p stash@{0}
  • 1
  • 2
  • 3
  • 4
# 删除所有存储的进度
$ git stash clear
  • 1
  • 2

当我们在当前分支开发某个需求的时候,遇到了另一个需求的联调问题,需要切换到另一个分支上去解决问题。怎

么办?正常情况下,我们应该将当前分支工作区的内容 add 、commit 之后再切换分支。但是问题来了,当前需求

开发了一半,我不想生成一次提交怎么办?放心,这个时候我们的 git stash 命令可以帮助我们将当前工作区的

内容储藏起来。然后切换其他分支,处理完问题之后,再切换到当前分支,执行 git stash pop 取出来就完事。

# 查看当前stash里边的内容
$ git stash list
# 将当前工作区内容储藏起来
$ git stash
# 恢复有两种方式
# 1、git stash apply恢复,恢复后stash内容并不删除,你需要使用命令git stash drop来删除
# 2、另一种方式是使用git stash pop,恢复的同时把stash内容也删除了,将stash中栈顶内容pop出来,当然也可以根据顺序直接取第n个
$ git stash pop
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

能够将所有未提交的修改保存至堆栈中,用于后续恢复当前工作区内容- 如果文件没有提交到暂存区(使用 git add .

追踪新的文件),使用该命令会提示 No local changes to save ,无法将修改保存到堆栈中。

使用场景:当你接到一个修复紧急 bug 的任务时候,一般都是先创建一个新的 bug 分支来修复它,然后合并,最

后删除。但是,如果当前你正在开发功能中,短时间还无法完成,无法直接提交到仓库,这时候可以先把当前工作

区的内容 git stash 一下,然后去修复 bug,修复后再 git stash pop,恢复之前的工作内容。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号