1GIT-RESET(1) Git Manual GIT-RESET(1)
2
3
4
6 git-reset - Reset current HEAD to the specified state
7
9 git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]
10 git reset [-q] [<commit>] [--] <paths>...
11 git reset --patch [<commit>] [--] [<paths>...]
12
13
15 Sets the current head to the specified commit and optionally resets the
16 index and working tree to match.
17
18 This command is useful if you notice some small error in a recent
19 commit (or set of commits) and want to redo that part without showing
20 the undo in the history.
21
22 If you want to undo a commit other than the latest on a branch, git-
23 revert(1) is your friend.
24
25 The second and third forms with paths and/or --patch are used to revert
26 selected paths in the index from a given commit, without moving HEAD.
27
29 --mixed
30 Resets the index but not the working tree (i.e., the changed files
31 are preserved but not marked for commit) and reports what has not
32 been updated. This is the default action.
33
34 --soft
35 Does not touch the index file nor the working tree at all, but
36 requires them to be in a good order. This leaves all your changed
37 files "Changes to be committed", as git status would put it.
38
39 --hard
40 Matches the working tree and index to that of the tree being
41 switched to. Any changes to tracked files in the working tree since
42 <commit> are lost.
43
44 --merge
45 Resets the index to match the tree recorded by the named commit,
46 and updates the files that are different between the named commit
47 and the current commit in the working tree.
48
49 --keep
50 Reset the index to the given commit, keeping local changes in the
51 working tree since the current commit, while updating working tree
52 files without local changes to what appears in the given commit. If
53 a file that is different between the current commit and the given
54 commit has local changes, reset is aborted.
55
56 -p, --patch
57 Interactively select hunks in the difference between the index and
58 <commit> (defaults to HEAD). The chosen hunks are applied in
59 reverse to the index.
60
61 This means that git reset -p is the opposite of git add -p (see
62 git-add(1)).
63
64 -q, --quiet
65 Be quiet, only report errors.
66
67 <commit>
68 Commit to make the current HEAD. If not given defaults to HEAD.
69
71 The tables below show what happens when running:
72
73 git reset --option target
74
75
76 to reset the HEAD to another commit (target) with the different reset
77 options depending on the state of the files.
78
79 In these tables, A, B, C and D are some different states of a file. For
80 example, the first line of the first table means that if a file is in
81 state A in the working tree, in state B in the index, in state C in
82 HEAD and in state D in the target, then "git reset --soft target" will
83 put the file in state A in the working tree, in state B in the index
84 and in state D in HEAD.
85
86 working index HEAD target working index HEAD
87 ----------------------------------------------------
88 A B C D --soft A B D
89 --mixed A D D
90 --hard D D D
91 --merge (disallowed)
92 --keep (disallowed)
93
94 working index HEAD target working index HEAD
95 ----------------------------------------------------
96 A B C C --soft A B C
97 --mixed A C C
98 --hard C C C
99 --merge (disallowed)
100 --keep A C C
101
102 working index HEAD target working index HEAD
103 ----------------------------------------------------
104 B B C D --soft B B D
105 --mixed B D D
106 --hard D D D
107 --merge D D D
108 --keep (disallowed)
109
110 working index HEAD target working index HEAD
111 ----------------------------------------------------
112 B B C C --soft B B C
113 --mixed B C C
114 --hard C C C
115 --merge C C C
116 --keep B C C
117
118 working index HEAD target working index HEAD
119 ----------------------------------------------------
120 B C C D --soft B C D
121 --mixed B D D
122 --hard D D D
123 --merge (disallowed)
124 --keep (disallowed)
125
126 working index HEAD target working index HEAD
127 ----------------------------------------------------
128 B C C C --soft B C C
129 --mixed B C C
130 --hard C C C
131 --merge B C C
132 --keep B C C
133
134 "reset --merge" is meant to be used when resetting out of a conflicted
135 merge. Any mergy operation guarantees that the work tree file that is
136 involved in the merge does not have local change wrt the index before
137 it starts, and that it writes the result out to the work tree. So if we
138 see some difference between the index and the target and also between
139 the index and the work tree, then it means that we are not resetting
140 out from a state that a mergy operation left after failing with a
141 conflict. That is why we disallow --merge option in this case.
142
143 "reset --keep" is meant to be used when removing some of the last
144 commits in the current branch while keeping changes in the working
145 tree. If there could be conflicts between the changes in the commit we
146 want to remove and the changes in the working tree we want to keep, the
147 reset is disallowed. That’s why it is disallowed if there are both
148 changes between the working tree and HEAD, and between HEAD and the
149 target. To be safe, it is also disallowed when there are unmerged
150 entries.
151
152 The following tables show what happens when there are unmerged entries:
153
154 working index HEAD target working index HEAD
155 ----------------------------------------------------
156 X U A B --soft (disallowed)
157 --mixed X B B
158 --hard B B B
159 --merge B B B
160 --keep (disallowed)
161
162 working index HEAD target working index HEAD
163 ----------------------------------------------------
164 X U A A --soft (disallowed)
165 --mixed X A A
166 --hard A A A
167 --merge A A A
168 --keep (disallowed)
169
170 X means any state and U means an unmerged index.
171
173 Undo a commit and redo
174
175 $ git commit ...
176 $ git reset --soft HEAD^ [1m(1)
177 $ edit [1m(2)
178 $ git commit -a -c ORIG_HEAD [1m(3)
179
180 1. This is most often done when you remembered what you just
181 committed is incomplete, or you misspelled your commit message, or
182 both. Leaves working tree as it was before "reset".
183 2. Make corrections to working tree files.
184 3. "reset" copies the old head to .git/ORIG_HEAD; redo the commit
185 by starting with its log message. If you do not need to edit the
186 message further, you can give -C option instead.
187
188 See also the --amend option to git-commit(1).
189
190 Undo commits permanently
191
192 $ git commit ...
193 $ git reset --hard HEAD~3 [1m(1)
194
195 1. The last three commits (HEAD, HEAD^, and HEAD~2) were bad and
196 you do not want to ever see them again. Do not do this if you have
197 already given these commits to somebody else. (See the "RECOVERING
198 FROM UPSTREAM REBASE" section in git-rebase(1) for the implications
199 of doing so.)
200
201 Undo a commit, making it a topic branch
202
203 $ git branch topic/wip [1m(1)
204 $ git reset --hard HEAD~3 [1m(2)
205 $ git checkout topic/wip [1m(3)
206
207 1. You have made some commits, but realize they were premature to
208 be in the "master" branch. You want to continue polishing them in a
209 topic branch, so create "topic/wip" branch off of the current HEAD.
210 2. Rewind the master branch to get rid of those three commits.
211 3. Switch to "topic/wip" branch and keep working.
212
213 Undo add
214
215 $ edit [1m(1)
216 $ git add frotz.c filfre.c
217 $ mailx [1m(2)
218 $ git reset [1m(3)
219 $ git pull git://info.example.com/ nitfol [1m(4)
220
221 1. You are happily working on something, and find the changes in
222 these files are in good order. You do not want to see them when you
223 run "git diff", because you plan to work on other files and changes
224 with these files are distracting.
225 2. Somebody asks you to pull, and the changes sounds worthy of
226 merging.
227 3. However, you already dirtied the index (i.e. your index does not
228 match the HEAD commit). But you know the pull you are going to make
229 does not affect frotz.c nor filfre.c, so you revert the index
230 changes for these two files. Your changes in working tree remain
231 there.
232 4. Then you can pull and merge, leaving frotz.c and filfre.c
233 changes still in the working tree.
234
235 Undo a merge or pull
236
237 $ git pull [1m(1)
238 Auto-merging nitfol
239 CONFLICT (content): Merge conflict in nitfol
240 Automatic merge failed; fix conflicts and then commit the result.
241 $ git reset --hard [1m(2)
242 $ git pull . topic/branch [1m(3)
243 Updating from 41223... to 13134...
244 Fast-forward
245 $ git reset --hard ORIG_HEAD [1m(4)
246
247 1. Try to update from the upstream resulted in a lot of conflicts;
248 you were not ready to spend a lot of time merging right now, so you
249 decide to do that later.
250 2. "pull" has not made merge commit, so "git reset --hard" which is
251 a synonym for "git reset --hard HEAD" clears the mess from the
252 index file and the working tree.
253 3. Merge a topic branch into the current branch, which resulted in
254 a fast-forward.
255 4. But you decided that the topic branch is not ready for public
256 consumption yet. "pull" or "merge" always leaves the original tip
257 of the current branch in ORIG_HEAD, so resetting hard to it brings
258 your index file and the working tree back to that state, and resets
259 the tip of the branch to that commit.
260
261 Undo a merge or pull inside a dirty work tree
262
263 $ git pull [1m(1)
264 Auto-merging nitfol
265 Merge made by recursive.
266 nitfol | 20 +++++----
267 ...
268 $ git reset --merge ORIG_HEAD [1m(2)
269
270 1. Even if you may have local modifications in your working tree,
271 you can safely say "git pull" when you know that the change in the
272 other branch does not overlap with them.
273 2. After inspecting the result of the merge, you may find that the
274 change in the other branch is unsatisfactory. Running "git reset
275 --hard ORIG_HEAD" will let you go back to where you were, but it
276 will discard your local changes, which you do not want. "git reset
277 --merge" keeps your local changes.
278
279 Interrupted workflow
280 Suppose you are interrupted by an urgent fix request while you are
281 in the middle of a large change. The files in your working tree are
282 not in any shape to be committed yet, but you need to get to the
283 other branch for a quick bugfix.
284
285 $ git checkout feature ;# you were working in "feature" branch and
286 $ work work work ;# got interrupted
287 $ git commit -a -m "snapshot WIP" [1m(1)
288 $ git checkout master
289 $ fix fix fix
290 $ git commit ;# commit with real log
291 $ git checkout feature
292 $ git reset --soft HEAD^ ;# go back to WIP state [1m(2)
293 $ git reset [1m(3)
294
295 1. This commit will get blown away so a throw-away log message is
296 OK.
297 2. This removes the WIP commit from the commit history, and sets
298 your working tree to the state just before you made that snapshot.
299 3. At this point the index file still has all the WIP changes you
300 committed as snapshot WIP. This updates the index to show your WIP
301 files as uncommitted.
302
303 See also git-stash(1).
304
305 Reset a single file in the index
306 Suppose you have added a file to your index, but later decide you
307 do not want to add it to your commit. You can remove the file from
308 the index while keeping your changes with git reset.
309
310 $ git reset -- frotz.c [1m(1)
311 $ git commit -m "Commit files in index" [1m(2)
312 $ git add frotz.c [1m(3)
313
314 1. This removes the file from the index while keeping it in the
315 working directory.
316 2. This commits all other changes in the index.
317 3. Adds the file to the index again.
318
319 Keep changes in working tree while discarding some previous commits
320 Suppose you are working on something and you commit it, and then
321 you continue working a bit more, but now you think that what you
322 have in your working tree should be in another branch that has
323 nothing to do with what you commited previously. You can start a
324 new branch and reset it while keeping the changes in your work
325 tree.
326
327 $ git tag start
328 $ git checkout -b branch1
329 $ edit
330 $ git commit ... [1m(1)
331 $ edit
332 $ git checkout -b branch2 [1m(2)
333 $ git reset --keep start [1m(3)
334
335 1. This commits your first edits in branch1.
336 2. In the ideal world, you could have realized that the earlier
337 commit did not belong to the new topic when you created and
338 switched to branch2 (i.e. "git checkout -b branch2 start"), but
339 nobody is perfect.
340 3. But you can use "reset --keep" to remove the unwanted commit
341 after you switched to "branch2".
342
344 Written by Junio C Hamano <gitster@pobox.com[1]> and Linus Torvalds
345 <torvalds@osdl.org[2]>
346
348 Documentation by Junio C Hamano and the git-list
349 <git@vger.kernel.org[3]>.
350
352 Part of the git(1) suite
353
355 1. gitster@pobox.com
356 mailto:gitster@pobox.com
357
358 2. torvalds@osdl.org
359 mailto:torvalds@osdl.org
360
361 3. git@vger.kernel.org
362 mailto:git@vger.kernel.org
363
364
365
366Git 1.7.1 08/16/2017 GIT-RESET(1)