Table of Contents

Stash


查看内容

# 查看一下工作区内容
E:\Dev\ParagonRoyalClient>git status # 工作区是干净的,没有新增或修改文件
On branch master
Your branch is up to date with 'origin/master'.
 
nothing to commit, working tree clean
 
E:\Dev\ParagonRoyalClient>git stash list # 可以查看现在Stash中存储了哪些内容,现在是空的
 
E:\Dev\ParagonRoyalClient>dir
 Volume in drive E is Mine
 Volume Serial Number is 4A5E-F784
 
 Directory of E:\Dev\ParagonRoyalClient # 文件目录
 
09/05/2022  10:21 AM    <DIR>          .
09/05/2022  10:21 AM    <DIR>          ..
09/01/2022  11:23 AM    <DIR>          ParagonRoyal
09/01/2022  11:22 AM            13,757 README.md
08/31/2022  02:33 PM    <DIR>          Source
               1 File(s)         13,757 bytes
               4 Dir(s)  937,662,742,528 bytes free

新增文件

# 对于新创建的文件
E:\Dev\ParagonRoyalClient>type nul > Feature_A.txt # 创建一个新文件
 
E:\Dev\ParagonRoyalClient>git status # 这个新文件是Untracked的状态
On branch master
Your branch is up to date with 'origin/master'.
 
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        Feature_A.txt
 
nothing added to commit but untracked files present (use "git add" to track)
 
E:\Dev\ParagonRoyalClient>git stash push # 没有Tracked的文件被修改,所以不能添加到Stash
No local changes to save
 
E:\Dev\ParagonRoyalClient>git add Feature_A.txt # Add新文件
 
E:\Dev\ParagonRoyalClient>git stash push # 这时候才能添加到Stash
Saved working directory and index state WIP on master: 8d358b2 upload project
 
E:\Dev\ParagonRoyalClient>git stash list # 添加成功
stash@{0}: WIP on master: 8d358b2 upload project

修改文件

# 对于修改文件
E:\Dev\ParagonRoyalClient>code README.md # 修改一个文件
 
E:\Dev\ParagonRoyalClient>git status # 文件被修改了,但是"not staged for commit"
On branch master
Your branch is up to date with 'origin/master'.
 
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:   README.md
 
no changes added to commit (use "git add" and/or "git commit -a")
 
E:\Dev\ParagonRoyalClient>git stash push # 这样的不需要Add就可以放到Stash中
Saved working directory and index state WIP on master: 8d358b2 upload project
 
E:\Dev\ParagonRoyalClient>git status # 修改没有了
On branch master
Your branch is up to date with 'origin/master'.
 
nothing to commit, working tree clean
 
# 如果修改的文件执行了Add,是Staged的状态呢?
E:\Dev\ParagonRoyalClient>git status
On branch master
Your branch is up to date with 'origin/master'.
 
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:   README.md
 
no changes added to commit (use "git add" and/or "git commit -a")
 
E:\Dev\ParagonRoyalClient>git add README.md # Add这个修改了的文件
 
E:\Dev\ParagonRoyalClient>git status
On branch master
Your branch is up to date with 'origin/master'.
 
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   README.md
 
 
E:\Dev\ParagonRoyalClient>git stash push # 可以放到Stash里面
Saved working directory and index state WIP on master: 8d358b2 upload project
 
E:\Dev\ParagonRoyalClient>git status # Add过的文件,修改也没有了
On branch master
Your branch is up to date with 'origin/master'.
 
nothing to commit, working tree clean
 
#总结:
# Stash会存储修改了并且被Tracked的文件,所以修改文件不需要Add(当然Add了也可以)。
# 但是对于新增加的文件,则需要Add一次,变为Tracked状态才行。

添加内容

# 使用Save
E:\Dev\ParagonRoyalClient>type nul > Feature_A.txt
 
E:\Dev\ParagonRoyalClient>git add Feature_A.txt
 
E:\Dev\ParagonRoyalClient>git stash save "Feature A" # Save的时候可以指定一条Message
 
E:\Dev\ParagonRoyalClient>git stash list
stash@{0}: On master: Feature A
 
E:\Dev\ParagonRoyalClient>git status # 可以看到修改被撤回了
On branch master
Your branch is up to date with 'origin/master'.
 
nothing to commit, working tree clean
 
# 使用Push
E:\Dev\ParagonRoyalClient>git status # 已经创建并Add了两个文件
On branch master
Your branch is up to date with 'origin/master'.
 
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   Feature_A.txt
        new file:   Feature_B.txt
 
 
# 使用-m指定Message,后面跟文件列表,表明哪些文件需要Stash
E:\Dev\ParagonRoyalClient>git stash push -m "Feature B" Feature_B.txt
Saved working directory and index state On master: Feature B
 
E:\Dev\ParagonRoyalClient>git status # 可以看到指定的文件的修改被撤回了
On branch master
Your branch is up to date with 'origin/master'.
 
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   Feature_A.txt
 
 
E:\Dev\ParagonRoyalClient>git stash list
stash@{0}: On master: Feature B
 
# 不跟参数的使用:两者都一样,把所有未Commit的修改放到Stash里,没有Message
E:\Dev\ParagonRoyalClient>git stash list
 
E:\Dev\ParagonRoyalClient>git status
On branch master
Your branch is up to date with 'origin/master'.
 
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   Feature_A.txt
        new file:   Feature_B.txt
 
 
E:\Dev\ParagonRoyalClient>git stash push # 不跟参数的Push
Saved working directory and index state WIP on master: 8d358b2 upload project
 
E:\Dev\ParagonRoyalClient>git status # 所有修改都进入Stash了
On branch master
Your branch is up to date with 'origin/master'.
 
nothing to commit, working tree clean
 
E:\Dev\ParagonRoyalClient>git stash list # 一条没有Message的记录
stash@{0}: WIP on master: 8d358b2 upload project
 
E:\Dev\ParagonRoyalClient>git stash pop
On branch master
Your branch is up to date with 'origin/master'.
 
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   Feature_A.txt
        new file:   Feature_B.txt
 
Dropped refs/stash@{0} (c8f3b6783844a74f0dc47b9c54e1dfd52698b14a)
 
E:\Dev\ParagonRoyalClient>git stash save # 不跟参数的Save
Saved working directory and index state WIP on master: 8d358b2 upload project
 
E:\Dev\ParagonRoyalClient>git stash list # 一条没有Message的记录
stash@{0}: WIP on master: 8d358b2 upload project
 
E:\Dev\ParagonRoyalClient>git status # 所有修改都进入Stash了
On branch master
Your branch is up to date with 'origin/master'.
 
nothing to commit, working tree clean
 
E:\Dev\ParagonRoyalClient>git stash pop
On branch master
Your branch is up to date with 'origin/master'.
 
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   Feature_A.txt
        new file:   Feature_B.txt
 
Dropped refs/stash@{0} (66b2e26b214218ef2b60d5d3f2dadf0f9285b321)

取出内容

# 使用Pop,就像栈的Pop
E:\Dev\ParagonRoyalClient>git stash pop
On branch master
Your branch is up to date with 'origin/master'.
 
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   Feature_B.txt
 
Dropped refs/stash@{0} (c879a1d7c42bdc8ed08c3e4b20c01ea7f25cbabc)
 
E:\Dev\ParagonRoyalClient>git stash list
stash@{0}: On master: A
 
# 使用Apply,就像栈的Top
E:\Dev\ParagonRoyalClient>git stash apply
On branch master
Your branch is up to date with 'origin/master'.
 
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   Feature_A.txt
        new file:   Feature_B.txt
 
 
E:\Dev\ParagonRoyalClient>git stash list
stash@{0}: On master: A

解决冲突

 

清除内容

# 清除某一个ID的内容
E:\Dev\ParagonRoyalClient>git stash list # 有3条记录
stash@{0}: On master: This is a Feature
stash@{1}: WIP on master: 8d358b2 upload project
stash@{2}: WIP on master: 8d358b2 upload project
 
E:\Dev\ParagonRoyalClient>git stash drop "stash@{2}" # 删除某一条记录
Dropped stash@{2} (8217e0e9a42252c5ddb387c3f07ab0875c754b20)
 
E:\Dev\ParagonRoyalClient>git stash list
stash@{0}: On master: This is a Feature
stash@{1}: WIP on master: 8d358b2 upload project
 
E:\Dev\ParagonRoyalClient>git stash drop # 删除最新的一条记录,也就是0
Dropped refs/stash@{0} (2e4c9abcac51b06aac8412ea98c5c4e78554e41c)
 
E:\Dev\ParagonRoyalClient>git stash list
stash@{0}: WIP on master: 8d358b2 upload project
 
# 清除所有保存的内容
E:\Dev\ParagonRoyalClient>git stash list
stash@{0}: WIP on master: 8d358b2 upload project
stash@{1}: WIP on master: 8d358b2 upload project
 
E:\Dev\ParagonRoyalClient>git stash clear # 清空
 
E:\Dev\ParagonRoyalClient>git stash list

在不同的分支中

 

总结