1GIT-RESET(1)                      Git Manual                      GIT-RESET(1)
2
3
4

NAME

6       git-reset - Reset current HEAD to the specified state
7

SYNOPSIS

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

DESCRIPTION

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>. Before the operation, ORIG_HEAD
51           is set to the tip of the current branch. If <mode> is omitted,
52           defaults to --mixed. The <mode> must be one of the following:
53
54           --soft
55               Does not touch the index file or the working tree at all (but
56               resets the head to <commit>, just like all modes do). This
57               leaves all your changed files "Changes to be committed", as git
58               status would put it.
59
60           --mixed
61               Resets the index but not the working tree (i.e., the changed
62               files are preserved but not marked for commit) and reports what
63               has not been updated. This is the default action.
64
65               If -N is specified, removed paths are marked as intent-to-add
66               (see git-add(1)).
67
68           --hard
69               Resets the index and working tree. Any changes to tracked files
70               in the working tree since <commit> are discarded. Any untracked
71               files or directories in the way of writing any tracked files
72               are simply deleted.
73
74           --merge
75               Resets the index and updates the files in the working tree that
76               are different between <commit> and HEAD, but keeps those which
77               are different between the index and working tree (i.e. which
78               have changes which have not been added). If a file that is
79               different between <commit> and the index has unstaged changes,
80               reset is aborted.
81
82               In other words, --merge does something like a git read-tree -u
83               -m <commit>, but carries forward unmerged index entries.
84
85           --keep
86               Resets index entries and updates files in the working tree that
87               are different between <commit> and HEAD. If a file that is
88               different between <commit> and HEAD has local changes, reset is
89               aborted.
90
91           --[no-]recurse-submodules
92               When the working tree is updated, using --recurse-submodules
93               will also recursively reset the working tree of all active
94               submodules according to the commit recorded in the
95               superproject, also setting the submodules' HEAD to be detached
96               at that commit.
97
98       See "Reset, restore and revert" in git(1) for the differences between
99       the three commands.
100

OPTIONS

102       -q, --quiet
103           Be quiet, only report errors.
104
105       --refresh, --no-refresh
106           Refresh the index after a mixed reset. Enabled by default.
107
108       --pathspec-from-file=<file>
109           Pathspec is passed in <file> instead of commandline args. If <file>
110           is exactly - then standard input is used. Pathspec elements are
111           separated by LF or CR/LF. Pathspec elements can be quoted as
112           explained for the configuration variable core.quotePath (see git-
113           config(1)). See also --pathspec-file-nul and global
114           --literal-pathspecs.
115
116       --pathspec-file-nul
117           Only meaningful with --pathspec-from-file. Pathspec elements are
118           separated with NUL character and all other characters are taken
119           literally (including newlines and quotes).
120
121       --
122           Do not interpret any more arguments as options.
123
124       <pathspec>...
125           Limits the paths affected by the operation.
126
127           For more details, see the pathspec entry in gitglossary(7).
128

EXAMPLES

130       Undo add
131
132               $ edit                                     (1)
133               $ git add frotz.c filfre.c
134               $ mailx                                    (2)
135               $ git reset                                (3)
136               $ git pull git://info.example.com/ nitfol  (4)
137
138            1. You are happily working on something, and find the changes
139               in these files are in good order. You do not want to see
140               them when you run git diff, because you plan to work on
141               other files and changes with these files are distracting.
142            2. Somebody asks you to pull, and the changes sound worthy of
143               merging.
144            3. However, you already dirtied the index (i.e. your index
145               does not match the HEAD commit). But you know the pull you
146               are going to make does not affect frotz.c or filfre.c, so
147               you revert the index changes for these two files. Your
148               changes in working tree remain there.
149            4. Then you can pull and merge, leaving frotz.c and filfre.c
150               changes still in the working tree.
151
152       Undo a commit and redo
153
154               $ git commit ...
155               $ git reset --soft HEAD^      (1)
156               $ edit                        (2)
157               $ git commit -a -c ORIG_HEAD  (3)
158
159
160            1. This is most often done when you remembered what you just
161               committed is incomplete, or you misspelled your commit
162               message, or both. Leaves working tree as it was before
163               "reset".
164            2. Make corrections to working tree files.
165            3. "reset" copies the old head to .git/ORIG_HEAD; redo the
166               commit by starting with its log message. If you do not
167               need to edit the message further, you can give -C option
168               instead.
169           See also the --amend option to git-commit(1).
170
171       Undo a commit, making it a topic branch
172
173               $ git branch topic/wip          (1)
174               $ git reset --hard HEAD~3       (2)
175               $ git switch topic/wip          (3)
176
177            1. You have made some commits, but realize they were
178               premature to be in the master branch. You want to continue
179               polishing them in a topic branch, so create topic/wip
180               branch off of the current HEAD.
181            2. Rewind the master branch to get rid of those three
182               commits.
183            3. Switch to topic/wip branch and keep working.
184
185       Undo commits permanently
186
187               $ git commit ...
188               $ git reset --hard HEAD~3   (1)
189
190            1. The last three commits (HEAD, HEAD^, and HEAD~2) were bad
191               and you do not want to ever see them again. Do not do this
192               if you have already given these commits to somebody else.
193               (See the "RECOVERING FROM UPSTREAM REBASE" section in git-
194               rebase(1) for the implications of doing so.)
195
196       Undo a merge or pull
197
198               $ git pull                         (1)
199               Auto-merging nitfol
200               CONFLICT (content): Merge conflict in nitfol
201               Automatic merge failed; fix conflicts and then commit the result.
202               $ git reset --hard                 (2)
203               $ git pull . topic/branch          (3)
204               Updating from 41223... to 13134...
205               Fast-forward
206               $ git reset --hard ORIG_HEAD       (4)
207
208            1. Try to update from the upstream resulted in a lot of
209               conflicts; you were not ready to spend a lot of time
210               merging right now, so you decide to do that later.
211            2. "pull" has not made merge commit, so git reset --hard
212               which is a synonym for git reset --hard HEAD clears the
213               mess from the index file and the working tree.
214            3. Merge a topic branch into the current branch, which
215               resulted in a fast-forward.
216            4. But you decided that the topic branch is not ready for
217               public consumption yet. "pull" or "merge" always leaves
218               the original tip of the current branch in ORIG_HEAD, so
219               resetting hard to it brings your index file and the
220               working tree back to that state, and resets the tip of the
221               branch to that commit.
222
223       Undo a merge or pull inside a dirty working tree
224
225               $ git pull                         (1)
226               Auto-merging nitfol
227               Merge made by recursive.
228                nitfol                |   20 +++++----
229                ...
230               $ git reset --merge ORIG_HEAD      (2)
231
232            1. Even if you may have local modifications in your working
233               tree, you can safely say git pull when you know that the
234               change in the other branch does not overlap with them.
235            2. After inspecting the result of the merge, you may find
236               that the change in the other branch is unsatisfactory.
237               Running git reset --hard ORIG_HEAD will let you go back to
238               where you were, but it will discard your local changes,
239               which you do not want.  git reset --merge keeps your local
240               changes.
241
242       Interrupted workflow
243           Suppose you are interrupted by an urgent fix request while you are
244           in the middle of a large change. The files in your working tree are
245           not in any shape to be committed yet, but you need to get to the
246           other branch for a quick bugfix.
247
248               $ git switch feature  ;# you were working in "feature" branch and
249               $ work work work      ;# got interrupted
250               $ git commit -a -m "snapshot WIP"                 (1)
251               $ git switch master
252               $ fix fix fix
253               $ git commit ;# commit with real log
254               $ git switch feature
255               $ git reset --soft HEAD^ ;# go back to WIP state  (2)
256               $ git reset                                       (3)
257
258
259            1. This commit will get blown away so a throw-away log
260               message is OK.
261            2. This removes the WIP commit from the commit history, and
262               sets your working tree to the state just before you made
263               that snapshot.
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                      (1)
276               $ git commit -m "Commit files in index"     (2)
277               $ git add frotz.c                           (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 ...                            (1)
296               $ edit
297               $ git switch -c branch2                     (2)
298               $ git reset --keep start                    (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^                        (1)
318               $ git add -p                                (2)
319               $ git diff --cached                         (3)
320               $ git commit -c HEAD@{1}                    (4)
321               ...                                         (5)
322               $ git add ...                               (6)
323               $ git diff --cached                         (7)
324               $ git commit ...                            (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

DISCUSSION

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

GIT

462       Part of the git(1) suite
463
464
465
466Git 2.43.0                        11/20/2023                      GIT-RESET(1)
Impressum