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 [-q] [<tree-ish>] [--] <pathspec>...
10 git reset [-q] [--pathspec-from-file=<file> [--pathspec-file-nul]] [<tree-ish>]
11 git reset (--patch | -p) [<tree-ish>] [--] [<pathspec>...]
12 git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]
13
15 In the first three forms, copy entries from <tree-ish> to the index. In
16 the last form, set the current branch head (HEAD) to <commit>,
17 optionally modifying index and working tree to match. The
18 <tree-ish>/<commit> defaults to HEAD in all forms.
19
20 git reset [-q] [<tree-ish>] [--] <pathspec>..., git reset [-q]
21 [--pathspec-from-file=<file> [--pathspec-file-nul]] [<tree-ish>]
22 These forms reset the index entries for all paths that match the
23 <pathspec> to their state at <tree-ish>. (It does not affect the
24 working tree or the current branch.)
25
26 This means that git reset <pathspec> is the opposite of git add
27 <pathspec>. This command is equivalent to git restore
28 [--source=<tree-ish>] --staged <pathspec>....
29
30 After running git reset <pathspec> to update the index entry, you
31 can use git-restore(1) to check the contents out of the index to
32 the working tree. Alternatively, using git-restore(1) and
33 specifying a commit with --source, you can copy the contents of a
34 path out of a commit to the index and to the working tree in one
35 go.
36
37 git reset (--patch | -p) [<tree-ish>] [--] [<pathspec>...]
38 Interactively select hunks in the difference between the index and
39 <tree-ish> (defaults to HEAD). The chosen hunks are applied in
40 reverse to the index.
41
42 This means that git reset -p is the opposite of git add -p, i.e.
43 you can use it to selectively reset hunks. See the “Interactive
44 Mode” section of git-add(1) to learn how to operate the --patch
45 mode.
46
47 git reset [<mode>] [<commit>]
48 This form resets the current branch head to <commit> and possibly
49 updates the index (resetting it to the tree of <commit>) and the
50 working tree depending on <mode>. If <mode> is omitted, defaults to
51 --mixed. The <mode> must be one of the following:
52
53 --soft
54 Does not touch the index file or the working tree at all (but
55 resets the head to <commit>, just like all modes do). This
56 leaves all your changed files "Changes to be committed", as git
57 status would put it.
58
59 --mixed
60 Resets the index but not the working tree (i.e., the changed
61 files are preserved but not marked for commit) and reports what
62 has not been updated. This is the default action.
63
64 If -N is specified, removed paths are marked as intent-to-add
65 (see git-add(1)).
66
67 --hard
68 Resets the index and working tree. Any changes to tracked files
69 in the working tree since <commit> are discarded.
70
71 --merge
72 Resets the index and updates the files in the working tree that
73 are different between <commit> and HEAD, but keeps those which
74 are different between the index and working tree (i.e. which
75 have changes which have not been added). If a file that is
76 different between <commit> and the index has unstaged changes,
77 reset is aborted.
78
79 In other words, --merge does something like a git read-tree -u
80 -m <commit>, but carries forward unmerged index entries.
81
82 --keep
83 Resets index entries and updates files in the working tree that
84 are different between <commit> and HEAD. If a file that is
85 different between <commit> and HEAD has local changes, reset is
86 aborted.
87
88 --[no-]recurse-submodules
89 When the working tree is updated, using --recurse-submodules
90 will also recursively reset the working tree of all active
91 submodules according to the commit recorded in the
92 superproject, also setting the submodules' HEAD to be detached
93 at that commit.
94
95 See "Reset, restore and revert" in git(1) for the differences between
96 the three commands.
97
99 -q, --quiet, --no-quiet
100 Be quiet, only report errors. The default behavior is set by the
101 reset.quiet config option. --quiet and --no-quiet will override
102 the default behavior.
103
104 --pathspec-from-file=<file>
105 Pathspec is passed in <file> instead of commandline args. If <file>
106 is exactly - then standard input is used. Pathspec elements are
107 separated by LF or CR/LF. Pathspec elements can be quoted as
108 explained for the configuration variable core.quotePath (see git-
109 config(1)). See also --pathspec-file-nul and global
110 --literal-pathspecs.
111
112 --pathspec-file-nul
113 Only meaningful with --pathspec-from-file. Pathspec elements are
114 separated with NUL character and all other characters are taken
115 literally (including newlines and quotes).
116
117 --
118 Do not interpret any more arguments as options.
119
120 <pathspec>...
121 Limits the paths affected by the operation.
122
123 For more details, see the pathspec entry in gitglossary(7).
124
126 Undo add
127
128 $ edit [1m(1)
129 $ git add frotz.c filfre.c
130 $ mailx [1m(2)
131 $ git reset [1m(3)
132 $ git pull git://info.example.com/ nitfol [1m(4)
133
134 1. You are happily working on something, and find the changes
135 in these files are in good order. You do not want to see
136 them when you run git diff, because you plan to work on
137 other files and changes with these files are distracting.
138 2. Somebody asks you to pull, and the changes sound worthy of
139 merging.
140 3. However, you already dirtied the index (i.e. your index
141 does not match the HEAD commit). But you know the pull you
142 are going to make does not affect frotz.c or filfre.c, so
143 you revert the index changes for these two files. Your
144 changes in working tree remain there.
145 4. Then you can pull and merge, leaving frotz.c and filfre.c
146 changes still in the working tree.
147
148 Undo a commit and redo
149
150 $ git commit ...
151 $ git reset --soft HEAD^ [1m(1)
152 $ edit [1m(2)
153 $ git commit -a -c ORIG_HEAD [1m(3)
154
155
156 1. This is most often done when you remembered what you just
157 committed is incomplete, or you misspelled your commit
158 message, or both. Leaves working tree as it was before
159 "reset".
160 2. Make corrections to working tree files.
161 3. "reset" copies the old head to .git/ORIG_HEAD; redo the
162 commit by starting with its log message. If you do not
163 need to edit the message further, you can give -C option
164 instead.
165 See also the --amend option to git-commit(1).
166
167 Undo a commit, making it a topic branch
168
169 $ git branch topic/wip [1m(1)
170 $ git reset --hard HEAD~3 [1m(2)
171 $ git switch topic/wip [1m(3)
172
173 1. You have made some commits, but realize they were
174 premature to be in the master branch. You want to continue
175 polishing them in a topic branch, so create topic/wip
176 branch off of the current HEAD.
177 2. Rewind the master branch to get rid of those three
178 commits.
179 3. Switch to topic/wip branch and keep working.
180
181 Undo commits permanently
182
183 $ git commit ...
184 $ git reset --hard HEAD~3 [1m(1)
185
186 1. The last three commits (HEAD, HEAD^, and HEAD~2) were bad
187 and you do not want to ever see them again. Do not do this
188 if you have already given these commits to somebody else.
189 (See the "RECOVERING FROM UPSTREAM REBASE" section in git-
190 rebase(1) for the implications of doing so.)
191
192 Undo a merge or pull
193
194 $ git pull [1m(1)
195 Auto-merging nitfol
196 CONFLICT (content): Merge conflict in nitfol
197 Automatic merge failed; fix conflicts and then commit the result.
198 $ git reset --hard [1m(2)
199 $ git pull . topic/branch [1m(3)
200 Updating from 41223... to 13134...
201 Fast-forward
202 $ git reset --hard ORIG_HEAD [1m(4)
203
204 1. Try to update from the upstream resulted in a lot of
205 conflicts; you were not ready to spend a lot of time
206 merging right now, so you decide to do that later.
207 2. "pull" has not made merge commit, so git reset --hard
208 which is a synonym for git reset --hard HEAD clears the
209 mess from the index file and the working tree.
210 3. Merge a topic branch into the current branch, which
211 resulted in a fast-forward.
212 4. But you decided that the topic branch is not ready for
213 public consumption yet. "pull" or "merge" always leaves
214 the original tip of the current branch in ORIG_HEAD, so
215 resetting hard to it brings your index file and the
216 working tree back to that state, and resets the tip of the
217 branch to that commit.
218
219 Undo a merge or pull inside a dirty working tree
220
221 $ git pull [1m(1)
222 Auto-merging nitfol
223 Merge made by recursive.
224 nitfol | 20 +++++----
225 ...
226 $ git reset --merge ORIG_HEAD [1m(2)
227
228 1. Even if you may have local modifications in your working
229 tree, you can safely say git pull when you know that the
230 change in the other branch does not overlap with them.
231 2. After inspecting the result of the merge, you may find
232 that the change in the other branch is unsatisfactory.
233 Running git reset --hard ORIG_HEAD will let you go back to
234 where you were, but it will discard your local changes,
235 which you do not want. git reset --merge keeps your local
236 changes.
237
238 Interrupted workflow
239 Suppose you are interrupted by an urgent fix request while you are
240 in the middle of a large change. The files in your working tree are
241 not in any shape to be committed yet, but you need to get to the
242 other branch for a quick bugfix.
243
244 $ git switch feature ;# you were working in "feature" branch and
245 $ work work work ;# got interrupted
246 $ git commit -a -m "snapshot WIP" [1m(1)
247 $ git switch master
248 $ fix fix fix
249 $ git commit ;# commit with real log
250 $ git switch feature
251 $ git reset --soft HEAD^ ;# go back to WIP state [1m(2)
252 $ git reset [1m(3)
253
254
255 1. This commit will get blown away so a throw-away log
256 message is OK.
257 2. This removes the WIP commit from the commit history, and
258 sets your working tree to the state just before you made
259 that snapshot.
260 3. At this point the index file still has all the WIP changes
261 you committed as snapshot WIP. This updates the index to
262 show your WIP files as uncommitted.
263 See also git-stash(1).
264
265 Reset a single file in the index
266 Suppose you have added a file to your index, but later decide you
267 do not want to add it to your commit. You can remove the file from
268 the index while keeping your changes with git reset.
269
270 $ git reset -- frotz.c [1m(1)
271 $ git commit -m "Commit files in index" [1m(2)
272 $ git add frotz.c [1m(3)
273
274 1. This removes the file from the index while keeping it in
275 the working directory.
276 2. This commits all other changes in the index.
277 3. Adds the file to the index again.
278
279 Keep changes in working tree while discarding some previous commits
280 Suppose you are working on something and you commit it, and then
281 you continue working a bit more, but now you think that what you
282 have in your working tree should be in another branch that has
283 nothing to do with what you committed previously. You can start a
284 new branch and reset it while keeping the changes in your working
285 tree.
286
287 $ git tag start
288 $ git switch -c branch1
289 $ edit
290 $ git commit ... [1m(1)
291 $ edit
292 $ git switch -c branch2 [1m(2)
293 $ git reset --keep start [1m(3)
294
295 1. This commits your first edits in branch1.
296 2. In the ideal world, you could have realized that the
297 earlier commit did not belong to the new topic when you
298 created and switched to branch2 (i.e. git switch -c
299 branch2 start), but nobody is perfect.
300 3. But you can use reset --keep to remove the unwanted commit
301 after you switched to branch2.
302
303 Split a commit apart into a sequence of commits
304 Suppose that you have created lots of logically separate changes
305 and committed them together. Then, later you decide that it might
306 be better to have each logical chunk associated with its own
307 commit. You can use git reset to rewind history without changing
308 the contents of your local files, and then successively use git add
309 -p to interactively select which hunks to include into each commit,
310 using git commit -c to pre-populate the commit message.
311
312 $ git reset -N HEAD^ [1m(1)
313 $ git add -p [1m(2)
314 $ git diff --cached [1m(3)
315 $ git commit -c HEAD@{1} [1m(4)
316 ... [1m(5)
317 $ git add ... [1m(6)
318 $ git diff --cached [1m(7)
319 $ git commit ... [1m(8)
320
321 1. First, reset the history back one commit so that we remove
322 the original commit, but leave the working tree with all
323 the changes. The -N ensures that any new files added with
324 HEAD are still marked so that git add -p will find them.
325 2. Next, we interactively select diff hunks to add using the
326 git add -p facility. This will ask you about each diff
327 hunk in sequence and you can use simple commands such as
328 "yes, include this", "No don’t include this" or even the
329 very powerful "edit" facility.
330
331 3. Once satisfied with the hunks you want to include, you
332 should verify what has been prepared for the first commit
333 by using git diff --cached. This shows all the changes
334 that have been moved into the index and are about to be
335 committed.
336 4. Next, commit the changes stored in the index. The -c
337 option specifies to pre-populate the commit message from
338 the original message that you started with in the first
339 commit. This is helpful to avoid retyping it. The HEAD@{1}
340 is a special notation for the commit that HEAD used to be
341 at prior to the original reset commit (1 change ago). See
342 git-reflog(1) for more details. You may also use any other
343 valid commit reference.
344 5. You can repeat steps 2-4 multiple times to break the
345 original code into any number of commits.
346 6. Now you’ve split out many of the changes into their own
347 commits, and might no longer use the patch mode of git
348 add, in order to select all remaining uncommitted changes.
349 7. Once again, check to verify that you’ve included what you
350 want to. You may also wish to verify that git diff doesn’t
351 show any remaining changes to be committed later.
352 8. And finally create the final commit.
353
355 The tables below show what happens when running:
356
357 git reset --option target
358
359 to reset the HEAD to another commit (target) with the different reset
360 options depending on the state of the files.
361
362 In these tables, A, B, C and D are some different states of a file. For
363 example, the first line of the first table means that if a file is in
364 state A in the working tree, in state B in the index, in state C in
365 HEAD and in state D in the target, then git reset --soft target will
366 leave the file in the working tree in state A and in the index in state
367 B. It resets (i.e. moves) the HEAD (i.e. the tip of the current branch,
368 if you are on one) to target (which has the file in state D).
369
370 working index HEAD target working index HEAD
371 ----------------------------------------------------
372 A B C D --soft A B D
373 --mixed A D D
374 --hard D D D
375 --merge (disallowed)
376 --keep (disallowed)
377
378 working index HEAD target working index HEAD
379 ----------------------------------------------------
380 A B C C --soft A B C
381 --mixed A C C
382 --hard C C C
383 --merge (disallowed)
384 --keep A C C
385
386 working index HEAD target working index HEAD
387 ----------------------------------------------------
388 B B C D --soft B B D
389 --mixed B D D
390 --hard D D D
391 --merge D D D
392 --keep (disallowed)
393
394 working index HEAD target working index HEAD
395 ----------------------------------------------------
396 B B C C --soft B B C
397 --mixed B C C
398 --hard C C C
399 --merge C C C
400 --keep B C C
401
402 working index HEAD target working index HEAD
403 ----------------------------------------------------
404 B C C D --soft B C D
405 --mixed B D D
406 --hard D D D
407 --merge (disallowed)
408 --keep (disallowed)
409
410 working index HEAD target working index HEAD
411 ----------------------------------------------------
412 B C C C --soft B C C
413 --mixed B C C
414 --hard C C C
415 --merge B C C
416 --keep B C C
417
418 reset --merge is meant to be used when resetting out of a conflicted
419 merge. Any mergy operation guarantees that the working tree file that
420 is involved in the merge does not have a local change with respect to
421 the index before it starts, and that it writes the result out to the
422 working tree. So if we see some difference between the index and the
423 target and also between the index and the working tree, then it means
424 that we are not resetting out from a state that a mergy operation left
425 after failing with a conflict. That is why we disallow --merge option
426 in this case.
427
428 reset --keep is meant to be used when removing some of the last commits
429 in the current branch while keeping changes in the working tree. If
430 there could be conflicts between the changes in the commit we want to
431 remove and the changes in the working tree we want to keep, the reset
432 is disallowed. That’s why it is disallowed if there are both changes
433 between the working tree and HEAD, and between HEAD and the target. To
434 be safe, it is also disallowed when there are unmerged entries.
435
436 The following tables show what happens when there are unmerged entries:
437
438 working index HEAD target working index HEAD
439 ----------------------------------------------------
440 X U A B --soft (disallowed)
441 --mixed X B B
442 --hard B B B
443 --merge B B B
444 --keep (disallowed)
445
446 working index HEAD target working index HEAD
447 ----------------------------------------------------
448 X U A A --soft (disallowed)
449 --mixed X A A
450 --hard A A A
451 --merge A A A
452 --keep (disallowed)
453
454 X means any state and U means an unmerged index.
455
457 Part of the git(1) suite
458
459
460
461Git 2.30.2 2021-03-08 GIT-RESET(1)