You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/07-git-tools/sections/stashing-cleaning.asc
+19-11Lines changed: 19 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,11 +5,21 @@ Often, when you’ve been working on part of your project, things are in a messy
5
5
The problem is, you don’t want to do a commit of half-done work just so you can get back to this point later.
6
6
The answer to this issue is the `git stash` command.
7
7
8
-
Stashing takes the dirty state of your working directory – that is, your modified tracked files and staged changes – and saves it on a stack of unfinished changes that you can reapply at any time.
8
+
Stashing takes the dirty state of your working directory -- that is, your modified tracked files and staged changes -- and saves it on a stack of unfinished changes that you can reapply at any time (even on a different branch).
9
+
10
+
[NOTE]
11
+
.Migrating to `git stash push`
12
+
====
13
+
As of late October 2017, there has been extensive discussion on the Git mailing list, wherein the command `git stash save` is being deprecated in favour of the existing alternative `git stash push`.
14
+
The main reason for this is that `git stash push` introduces the option of stashing selected _pathspecs_, something `git stash save` does not support.
15
+
16
+
`git stash save` is not going away any time soon, so don't worry about it suddenly disappearing.
17
+
But you might want to start migrating over to the `push` alternative for the new functionality.
18
+
====
9
19
10
20
==== Stashing Your Work
11
21
12
-
To demonstrate, you’ll go into your project and start working on a couple of files and possibly stage one of the changes.
22
+
To demonstrate stashing, you’ll go into your project and start working on a couple of files and possibly stage one of the changes.
13
23
If you run `git status`, you can see your dirty state:
14
24
15
25
[source,console]
@@ -39,7 +49,7 @@ HEAD is now at 049d078 added the index file
39
49
(To restore them type "git stash apply")
40
50
----
41
51
42
-
Your working directory is clean:
52
+
You can now see that your working directory is clean:
43
53
44
54
[source,console]
45
55
----
@@ -48,7 +58,7 @@ $ git status
48
58
nothing to commit, working directory clean
49
59
----
50
60
51
-
At this point, you can easily switch branches and do work elsewhere; your changes are stored on your stack.
61
+
At this point, you can switch branches and do work elsewhere; your changes are stored on your stack.
52
62
To see which stashes you’ve stored, you can use `git stash list`:
53
63
54
64
[source,console]
@@ -82,7 +92,7 @@ You can see that Git re-modifies the files you reverted when you saved the stash
82
92
In this case, you had a clean working directory when you tried to apply the stash, and you tried to apply it on the same branch you saved it from.
83
93
Having a clean working directory and applying it on the same branch aren’t necessary to successfully apply a stash.
84
94
You can save a stash on one branch, switch to another branch later, and try to reapply the changes.
85
-
You can also have modified and uncommitted files in your working directory when you apply a stash – Git gives you merge conflicts if anything no longer applies cleanly.
95
+
You can also have modified and uncommitted files in your working directory when you apply a stash -- Git gives you merge conflicts if anything no longer applies cleanly.
86
96
87
97
The changes to your files were reapplied, but the file you staged before wasn’t restaged.
88
98
To do that, you must run the `git stash apply` command with a `--index` option to tell the command to try to reapply the staged changes.
@@ -104,7 +114,7 @@ Changes not staged for commit:
104
114
modified: lib/simplegit.rb
105
115
----
106
116
107
-
The apply option only tries to apply the stashed work – you continue to have it on your stack.
117
+
The apply option only tries to apply the stashed work -- you continue to have it on your stack.
108
118
To remove it, you can run `git stash drop` with the name of the stash to remove:
109
119
110
120
[source,console]
@@ -123,9 +133,7 @@ You can also run `git stash pop` to apply the stash and then immediately drop it
123
133
124
134
There are a few stash variants that may also be helpful.
125
135
The first option that is quite popular is the `--keep-index` option to the `stash save` command.
126
-
This tells Git to not stash anything that you've already staged with the `git add` command.
127
-
128
-
This can be really helpful if you've made a number of changes but want to only commit some of them and then come back to the rest of the changes at a later time.
136
+
This tells Git to not only include all staged content in the stash being created, but simultaneously leave it in the index.
129
137
130
138
[source,console]
131
139
----
@@ -142,8 +150,8 @@ M index.html
142
150
----
143
151
144
152
Another common thing you may want to do with stash is to stash the untracked files as well as the tracked ones.
145
-
By default, `git stash` will only store files that are already in the index.
146
-
If you specify `--include-untracked` or `-u`, Git will also stash any untracked files you have created.
153
+
By default, `git stash` will stash only modified and staged _tracked_ files.
154
+
If you specify `--include-untracked` or `-u`, Git will include untracked files in the stash being created.
0 commit comments