]>
Commit | Line | Data |
---|---|---|
09ccdb63 NS |
1 | git-stash(1) |
2 | ============ | |
3 | ||
4 | NAME | |
5 | ---- | |
6 | git-stash - Stash the changes in a dirty working directory away | |
7 | ||
8 | SYNOPSIS | |
9 | -------- | |
10 | [verse] | |
b1889c36 JN |
11 | 'git stash' (list | show [<stash>] | apply [<stash>] | clear | drop [<stash>] | pop [<stash>]) |
12 | 'git stash' [save [<message>]] | |
09ccdb63 NS |
13 | |
14 | DESCRIPTION | |
15 | ----------- | |
16 | ||
b1889c36 | 17 | Use 'git stash' when you want to record the current state of the |
09ccdb63 NS |
18 | working directory and the index, but want to go back to a clean |
19 | working directory. The command saves your local modifications away | |
20 | and reverts the working directory to match the `HEAD` commit. | |
21 | ||
22 | The modifications stashed away by this command can be listed with | |
483bc4f0 JN |
23 | `git stash list`, inspected with `git stash show`, and restored |
24 | (potentially on top of a different commit) with `git stash apply`. | |
25 | Calling `git stash` without any arguments is equivalent to `git stash save`. | |
26 | A stash is by default listed as "WIP on 'branchname' ...", but | |
ec96e0f6 NS |
27 | you can give a more descriptive message on the command line when |
28 | you create one. | |
09ccdb63 NS |
29 | |
30 | The latest stash you created is stored in `$GIT_DIR/refs/stash`; older | |
9488e875 | 31 | stashes are found in the reflog of this reference and can be named using |
e2c6de1c SH |
32 | the usual reflog syntax (e.g. `stash@\{0}` is the most recently |
33 | created stash, `stash@\{1}` is the one before it, `stash@\{2.hours.ago}` | |
9488e875 | 34 | is also possible). |
09ccdb63 NS |
35 | |
36 | OPTIONS | |
37 | ------- | |
38 | ||
71bda8b9 | 39 | save [<message>]:: |
09ccdb63 | 40 | |
b1889c36 | 41 | Save your local modifications to a new 'stash', and run `git reset |
fcb10a96 | 42 | --hard` to revert them. This is the default action when no |
71bda8b9 JA |
43 | subcommand is given. The <message> part is optional and gives |
44 | the description along with the stashed state. | |
09ccdb63 | 45 | |
fbd538c2 | 46 | list [<options>]:: |
09ccdb63 NS |
47 | |
48 | List the stashes that you currently have. Each 'stash' is listed | |
36717575 | 49 | with its name (e.g. `stash@\{0}` is the latest stash, `stash@\{1}` is |
9488e875 | 50 | the one before, etc.), the name of the branch that was current when the |
09ccdb63 NS |
51 | stash was made, and a short description of the commit the stash was |
52 | based on. | |
53 | + | |
54 | ---------------------------------------------------------------- | |
ec96e0f6 NS |
55 | stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation |
56 | stash@{1}: On master: 9cc0589... Add git-stash | |
09ccdb63 | 57 | ---------------------------------------------------------------- |
fbd538c2 | 58 | + |
483bc4f0 JN |
59 | The command takes options applicable to the `git-log` |
60 | command to control what is shown and how. See linkgit:git-log[1]. | |
09ccdb63 NS |
61 | |
62 | show [<stash>]:: | |
63 | ||
06ada152 | 64 | Show the changes recorded in the stash as a diff between the |
9488e875 JH |
65 | stashed state and its original parent. When no `<stash>` is given, |
66 | shows the latest one. By default, the command shows the diffstat, but | |
b1889c36 | 67 | it will accept any format known to `git-diff` (e.g., `git stash show |
e2c6de1c | 68 | -p stash@\{1}` to view the second most recent stash in patch form). |
09ccdb63 | 69 | |
0bdcac56 | 70 | apply [--index] [<stash>]:: |
09ccdb63 | 71 | |
9488e875 | 72 | Restore the changes recorded in the stash on top of the current |
09ccdb63 | 73 | working tree state. When no `<stash>` is given, applies the latest |
9488e875 JH |
74 | one. The working directory must match the index. |
75 | + | |
76 | This operation can fail with conflicts; you need to resolve them | |
77 | by hand in the working tree. | |
0bdcac56 MV |
78 | + |
79 | If the `--index` option is used, then tries to reinstate not only the working | |
80 | tree's changes, but also the index's ones. However, this can fail, when you | |
81 | have conflicts (which are stored in the index, where you therefore can no | |
82 | longer apply the changes as they were originally). | |
09ccdb63 NS |
83 | |
84 | clear:: | |
9488e875 JH |
85 | Remove all the stashed states. Note that those states will then |
86 | be subject to pruning, and may be difficult or impossible to recover. | |
09ccdb63 | 87 | |
e25d5f9c BC |
88 | drop [<stash>]:: |
89 | ||
90 | Remove a single stashed state from the stash list. When no `<stash>` | |
91 | is given, it removes the latest one. i.e. `stash@\{0}` | |
92 | ||
bd56ff54 BC |
93 | pop [<stash>]:: |
94 | ||
95 | Remove a single stashed state from the stash list and apply on top | |
96 | of the current working tree state. When no `<stash>` is given, | |
97 | `stash@\{0}` is assumed. See also `apply`. | |
98 | ||
09ccdb63 NS |
99 | |
100 | DISCUSSION | |
101 | ---------- | |
102 | ||
103 | A stash is represented as a commit whose tree records the state of the | |
104 | working directory, and its first parent is the commit at `HEAD` when | |
105 | the stash was created. The tree of the second parent records the | |
106 | state of the index when the stash is made, and it is made a child of | |
107 | the `HEAD` commit. The ancestry graph looks like this: | |
108 | ||
109 | .----W | |
110 | / / | |
114fd812 | 111 | -----H----I |
09ccdb63 NS |
112 | |
113 | where `H` is the `HEAD` commit, `I` is a commit that records the state | |
114 | of the index, and `W` is a commit that records the state of the working | |
115 | tree. | |
116 | ||
117 | ||
118 | EXAMPLES | |
119 | -------- | |
120 | ||
121 | Pulling into a dirty tree:: | |
122 | ||
123 | When you are in the middle of something, you learn that there are | |
9488e875 JH |
124 | upstream changes that are possibly relevant to what you are |
125 | doing. When your local changes do not conflict with the changes in | |
09ccdb63 NS |
126 | the upstream, a simple `git pull` will let you move forward. |
127 | + | |
128 | However, there are cases in which your local changes do conflict with | |
129 | the upstream changes, and `git pull` refuses to overwrite your | |
9488e875 | 130 | changes. In such a case, you can stash your changes away, |
09ccdb63 NS |
131 | perform a pull, and then unstash, like this: |
132 | + | |
133 | ---------------------------------------------------------------- | |
134 | $ git pull | |
135 | ... | |
136 | file foobar not up to date, cannot merge. | |
137 | $ git stash | |
138 | $ git pull | |
139 | $ git stash apply | |
140 | ---------------------------------------------------------------- | |
141 | ||
142 | Interrupted workflow:: | |
143 | ||
144 | When you are in the middle of something, your boss comes in and | |
9488e875 | 145 | demands that you fix something immediately. Traditionally, you would |
09ccdb63 | 146 | make a commit to a temporary branch to store your changes away, and |
9488e875 | 147 | return to your original branch to make the emergency fix, like this: |
09ccdb63 NS |
148 | + |
149 | ---------------------------------------------------------------- | |
150 | ... hack hack hack ... | |
151 | $ git checkout -b my_wip | |
152 | $ git commit -a -m "WIP" | |
153 | $ git checkout master | |
154 | $ edit emergency fix | |
155 | $ git commit -a -m "Fix in a hurry" | |
156 | $ git checkout my_wip | |
157 | $ git reset --soft HEAD^ | |
158 | ... continue hacking ... | |
159 | ---------------------------------------------------------------- | |
160 | + | |
161 | You can use `git-stash` to simplify the above, like this: | |
162 | + | |
163 | ---------------------------------------------------------------- | |
164 | ... hack hack hack ... | |
165 | $ git stash | |
166 | $ edit emergency fix | |
167 | $ git commit -a -m "Fix in a hurry" | |
168 | $ git stash apply | |
169 | ... continue hacking ... | |
170 | ---------------------------------------------------------------- | |
171 | ||
172 | SEE ALSO | |
173 | -------- | |
5162e697 DM |
174 | linkgit:git-checkout[1], |
175 | linkgit:git-commit[1], | |
176 | linkgit:git-reflog[1], | |
177 | linkgit:git-reset[1] | |
09ccdb63 NS |
178 | |
179 | AUTHOR | |
180 | ------ | |
181 | Written by Nanako Shiraishi <nanako3@bluebottle.com> | |
182 | ||
183 | GIT | |
184 | --- | |
9e1f0a85 | 185 | Part of the linkgit:git[1] suite |