git stash

Git stash

Definition

Definition (定义)

The git stash command shelves changes you have made to your working copy so you can do another work, and then come back and re-apply them.

Stashing your work

Stashing your work (存放您的作品)

The git stash takes uncommitted both staged and unstaged changes, saves them away for further use, and then returns them from your working copy. Firstly, you can run the git status so you can see the dirty state. Then run git stash to stash the changes:

git status
On branch master
Changes to be committed:
new file: style.css
Changes not staged for commit:
modified: index.html
$ git stash
Saved working directory and index state WIP on master: 5002d47 our new homepage
HEAD is now at 5002d47 our new homepage
$ git status
On branch master
nothing to commit, working tree clean

Re-applying your stashed changes

Re-applying your stashed changes (重新应用您隐藏的更改)

The git stash pop removes the changes from your stash and re-applies them to your working copy.

The alternate way is running git stash apply if you want to re-apply the changes and keep them in your stash:

git status
On branch master
nothing to commit, working tree clean
git stash pop
On branch master
Changes to be committed:
new file: style.css
Changes not staged for commit:
modified: index.html
Dropped refs/stash@{0} (32b3aa1d185dfe6d57b3c3cc3b32cbf3e380cc6a)

Stashing untracked or ignored files

Stashing untracked or ignored files (存储未跟踪或忽略的文件)

The git stash will stash the changes that have been added to your index (staged changes) and changes made to files currently tracked by Git (unstaged changes). It will not stash the new files in the working copy that have not yet been staged and ignored files. In these cases, the git stash -u option (or –include-untracked) helps to stash the untracked files.

git stash -u

You can add changes to ignored files as well by using the -a option (or –all) when running git stash.

git stash -a

Multiple Stashes

Multiple Stashes (多个藏匿处)

You can run git stash several times so as to create multiple stashes, and then run git stash list to view them. By default, stashes are identified as a “WIP” – work in progress. It comes on top of the branch and commits that you created the stash from.

git stash list
stash@{0}: WIP on master: 5002d47 our new homepage
stash@{1}: WIP on master: 5002d47 our new homepage
stash@{2}: WIP on master: 5002d47 our new homepage

It is good to add some context with git stash save “message”

By default, git stash pop will re-apply the last created stash: stash@{0}

You can choose which stash to re-apply like this:

git stash pop stash@{3}

Viewing stash diffs

Use git stash show to view a summary of a stash:

git stash show
index.html | 1 +
style.css | 3 +++
2 files changed, 4 insertions(+)

You can also use the -p or –patch options to see the full diff of a stash:

git stash show -p
diff --git a/style.css b/style.css
new file mode 100644
index 0000000..d92368b
--- /dev/null
+++ b/style.css
@@ -0,0 +1,3 @@
+* {
+ text-decoration: blink;
+}
diff --git a/index.html b/index.html
index 9daeafb..ebdcbd2 100644
--- a/index.html
+++ b/index.html
@@ -1 +1,2 @@
+

Partial stashes

Partial stashes (部分藏匿)

Git allows choosing whether you want to stash just a single file, a bunch of files or individual changes within files. The git stash-p iterates through each hunk (a piece of change in Git) in the working copy and ask if you want to stash it or not:

git stash -p
diff --git a/style.css b/style.css
new file mode 100644
index 0000000..d92368b
--- /dev/null
+++ b/style.css
@@ -0,0 +1,3 @@
+* {
+ text-decoration: blink;
+}
Stash this hunk [y,n,q,a,d,/,e,?]? y
diff --git a/index.html b/index.html
index 9daeafb..ebdcbd2 100644
--- a/index.html
+++ b/index.html
@@ -1 +1,2 @@
+
Stash this hunk [y,n,q,a,d,/,e,?]? n

Hunk Commands

CommandDescription
/Search for a hunk by regex.
?Print help.
nDo not stash the hunk.
aStash this hunk and all later hunks in the file.
dDo not stash this hunk or any of the later hunks in the file.
eManually edit the current hunk
qQuit (selected hunks will be stashed)
sSplit the hunk into smaller hunks.
yStash the hunk.

Creating a branch from stash

Creating a branch from stash (从stash创建分支)

You can create a new branch to apply your stashed changes to it with git stash branch:

git stash branch add-stylesheet stash@{1}
Switched to a new branch 'add-stylesheet'
On branch add-stylesheet
Changes to be committed:
new file: style.css
Changes not staged for commit:
modified: index.html
Dropped refs/stash@{1} (32b3aa1d185dfe6d57b3c3cc3b32cbf3e380cc6a)

Cleaning up the stash

Cleaning up the stash (清理藏匿处)

You can delete the stash with git stash drop:

git stash drop stash@{1}
Dropped stash@{1} (17e2697fd8251df6163117cb3d58c1f62a5e7cdb)

If you use git stash clear it will delete all the stashes:

git stash clear


请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部