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