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>. 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       See "Reset, restore and revert" in git(1) for the differences between
89       the three commands.
90

OPTIONS

92       -q, --quiet, --no-quiet
93           Be quiet, only report errors. The default behavior is set by the
94           reset.quiet config option.  --quiet and --no-quiet will override
95           the default behavior.
96
97       --pathspec-from-file=<file>
98           Pathspec is passed in <file> instead of commandline args. If <file>
99           is exactly - then standard input is used. Pathspec elements are
100           separated by LF or CR/LF. Pathspec elements can be quoted as
101           explained for the configuration variable core.quotePath (see git-
102           config(1)). See also --pathspec-file-nul and global
103           --literal-pathspecs.
104
105       --pathspec-file-nul
106           Only meaningful with --pathspec-from-file. Pathspec elements are
107           separated with NUL character and all other characters are taken
108           literally (including newlines and quotes).
109
110       --
111           Do not interpret any more arguments as options.
112
113       <pathspec>...
114           Limits the paths affected by the operation.
115
116           For more details, see the pathspec entry in gitglossary(7).
117

EXAMPLES

119       Undo add
120
121               $ edit                                     (1)
122               $ git add frotz.c filfre.c
123               $ mailx                                    (2)
124               $ git reset                                (3)
125               $ git pull git://info.example.com/ nitfol  (4)
126
127            1. You are happily working on something, and find the changes
128               in these files are in good order. You do not want to see
129               them when you run git diff, because you plan to work on
130               other files and changes with these files are distracting.
131
132
133            2. Somebody asks you to pull, and the changes sound worthy of
134               merging.
135            3. However, you already dirtied the index (i.e. your index
136               does not match the HEAD commit). But you know the pull you
137               are going to make does not affect frotz.c or filfre.c, so
138               you revert the index changes for these two files. Your
139               changes in working tree remain there.
140            4. Then you can pull and merge, leaving frotz.c and filfre.c
141               changes still in the working tree.
142
143       Undo a commit and redo
144
145               $ git commit ...
146               $ git reset --soft HEAD^      (1)
147               $ edit                        (2)
148               $ git commit -a -c ORIG_HEAD  (3)
149
150
151            1. This is most often done when you remembered what you just
152               committed is incomplete, or you misspelled your commit
153               message, or both. Leaves working tree as it was before
154               "reset".
155            2. Make corrections to working tree files.
156            3. "reset" copies the old head to .git/ORIG_HEAD; redo the
157               commit by starting with its log message. If you do not
158               need to edit the message further, you can give -C option
159               instead.
160           See also the --amend option to git-commit(1).
161
162       Undo a commit, making it a topic branch
163
164               $ git branch topic/wip          (1)
165               $ git reset --hard HEAD~3       (2)
166               $ git switch topic/wip          (3)
167
168            1. You have made some commits, but realize they were
169               premature to be in the master branch. You want to continue
170               polishing them in a topic branch, so create topic/wip
171               branch off of the current HEAD.
172            2. Rewind the master branch to get rid of those three
173               commits.
174            3. Switch to topic/wip branch and keep working.
175
176       Undo commits permanently
177
178               $ git commit ...
179               $ git reset --hard HEAD~3   (1)
180
181            1. The last three commits (HEAD, HEAD^, and HEAD~2) were bad
182               and you do not want to ever see them again. Do not do this
183               if you have already given these commits to somebody else.
184               (See the "RECOVERING FROM UPSTREAM REBASE" section in git-
185               rebase(1) for the implications of doing so.)
186
187       Undo a merge or pull
188
189               $ git pull                         (1)
190               Auto-merging nitfol
191               CONFLICT (content): Merge conflict in nitfol
192               Automatic merge failed; fix conflicts and then commit the result.
193               $ git reset --hard                 (2)
194               $ git pull . topic/branch          (3)
195               Updating from 41223... to 13134...
196               Fast-forward
197               $ git reset --hard ORIG_HEAD       (4)
198
199            1. Try to update from the upstream resulted in a lot of
200               conflicts; you were not ready to spend a lot of time
201               merging right now, so you decide to do that later.
202            2. "pull" has not made merge commit, so git reset --hard
203               which is a synonym for git reset --hard HEAD clears the
204               mess from the index file and the working tree.
205            3. Merge a topic branch into the current branch, which
206               resulted in a fast-forward.
207            4. But you decided that the topic branch is not ready for
208               public consumption yet. "pull" or "merge" always leaves
209               the original tip of the current branch in ORIG_HEAD, so
210               resetting hard to it brings your index file and the
211               working tree back to that state, and resets the tip of the
212               branch to that commit.
213
214       Undo a merge or pull inside a dirty working tree
215
216               $ git pull                         (1)
217               Auto-merging nitfol
218               Merge made by recursive.
219                nitfol                |   20 +++++----
220                ...
221               $ git reset --merge ORIG_HEAD      (2)
222
223            1. Even if you may have local modifications in your working
224               tree, you can safely say git pull when you know that the
225               change in the other branch does not overlap with them.
226            2. After inspecting the result of the merge, you may find
227               that the change in the other branch is unsatisfactory.
228               Running git reset --hard ORIG_HEAD will let you go back to
229               where you were, but it will discard your local changes,
230               which you do not want.  git reset --merge keeps your local
231               changes.
232
233       Interrupted workflow
234           Suppose you are interrupted by an urgent fix request while you are
235           in the middle of a large change. The files in your working tree are
236           not in any shape to be committed yet, but you need to get to the
237           other branch for a quick bugfix.
238
239               $ git switch feature  ;# you were working in "feature" branch and
240               $ work work work      ;# got interrupted
241               $ git commit -a -m "snapshot WIP"                 (1)
242               $ git switch master
243               $ fix fix fix
244               $ git commit ;# commit with real log
245               $ git switch feature
246               $ git reset --soft HEAD^ ;# go back to WIP state  (2)
247               $ git reset                                       (3)
248
249
250            1. This commit will get blown away so a throw-away log
251               message is OK.
252            2. This removes the WIP commit from the commit history, and
253               sets your working tree to the state just before you made
254               that snapshot.
255            3. At this point the index file still has all the WIP changes
256               you committed as snapshot WIP. This updates the index to
257               show your WIP files as uncommitted.
258           See also git-stash(1).
259
260       Reset a single file in the index
261           Suppose you have added a file to your index, but later decide you
262           do not want to add it to your commit. You can remove the file from
263           the index while keeping your changes with git reset.
264
265               $ git reset -- frotz.c                      (1)
266               $ git commit -m "Commit files in index"     (2)
267               $ git add frotz.c                           (3)
268
269            1. This removes the file from the index while keeping it in
270               the working directory.
271            2. This commits all other changes in the index.
272            3. Adds the file to the index again.
273
274       Keep changes in working tree while discarding some previous commits
275           Suppose you are working on something and you commit it, and then
276           you continue working a bit more, but now you think that what you
277           have in your working tree should be in another branch that has
278           nothing to do with what you committed previously. You can start a
279           new branch and reset it while keeping the changes in your working
280           tree.
281
282               $ git tag start
283               $ git switch -c branch1
284               $ edit
285               $ git commit ...                            (1)
286               $ edit
287               $ git switch -c branch2                     (2)
288               $ git reset --keep start                    (3)
289
290            1. This commits your first edits in branch1.
291            2. In the ideal world, you could have realized that the
292               earlier commit did not belong to the new topic when you
293               created and switched to branch2 (i.e.  git switch -c
294               branch2 start), but nobody is perfect.
295            3. But you can use reset --keep to remove the unwanted commit
296               after you switched to branch2.
297
298       Split a commit apart into a sequence of commits
299           Suppose that you have created lots of logically separate changes
300           and committed them together. Then, later you decide that it might
301           be better to have each logical chunk associated with its own
302           commit. You can use git reset to rewind history without changing
303           the contents of your local files, and then successively use git add
304           -p to interactively select which hunks to include into each commit,
305           using git commit -c to pre-populate the commit message.
306
307               $ git reset -N HEAD^                        (1)
308               $ git add -p                                (2)
309               $ git diff --cached                         (3)
310               $ git commit -c HEAD@{1}                    (4)
311               ...                                         (5)
312               $ git add ...                               (6)
313               $ git diff --cached                         (7)
314               $ git commit ...                            (8)
315
316            1. First, reset the history back one commit so that we remove
317               the original commit, but leave the working tree with all
318               the changes. The -N ensures that any new files added with
319               HEAD are still marked so that git add -p will find them.
320            2. Next, we interactively select diff hunks to add using the
321               git add -p facility. This will ask you about each diff
322               hunk in sequence and you can use simple commands such as
323               "yes, include this", "No don’t include this" or even the
324               very powerful "edit" facility.
325            3. Once satisfied with the hunks you want to include, you
326               should verify what has been prepared for the first commit
327               by using git diff --cached. This shows all the changes
328               that have been moved into the index and are about to be
329               committed.
330
331            4. Next, commit the changes stored in the index. The -c
332               option specifies to pre-populate the commit message from
333               the original message that you started with in the first
334               commit. This is helpful to avoid retyping it. The HEAD@{1}
335               is a special notation for the commit that HEAD used to be
336               at prior to the original reset commit (1 change ago). See
337               git-reflog(1) for more details. You may also use any other
338               valid commit reference.
339            5. You can repeat steps 2-4 multiple times to break the
340               original code into any number of commits.
341            6. Now you’ve split out many of the changes into their own
342               commits, and might no longer use the patch mode of git
343               add, in order to select all remaining uncommitted changes.
344            7. Once again, check to verify that you’ve included what you
345               want to. You may also wish to verify that git diff doesn’t
346               show any remaining changes to be committed later.
347            8. And finally create the final commit.
348

DISCUSSION

350       The tables below show what happens when running:
351
352           git reset --option target
353
354       to reset the HEAD to another commit (target) with the different reset
355       options depending on the state of the files.
356
357       In these tables, A, B, C and D are some different states of a file. For
358       example, the first line of the first table means that if a file is in
359       state A in the working tree, in state B in the index, in state C in
360       HEAD and in state D in the target, then git reset --soft target will
361       leave the file in the working tree in state A and in the index in state
362       B. It resets (i.e. moves) the HEAD (i.e. the tip of the current branch,
363       if you are on one) to target (which has the file in state D).
364
365           working index HEAD target         working index HEAD
366           ----------------------------------------------------
367            A       B     C    D     --soft   A       B     D
368                                     --mixed  A       D     D
369                                     --hard   D       D     D
370                                     --merge (disallowed)
371                                     --keep  (disallowed)
372
373           working index HEAD target         working index HEAD
374           ----------------------------------------------------
375            A       B     C    C     --soft   A       B     C
376                                     --mixed  A       C     C
377                                     --hard   C       C     C
378                                     --merge (disallowed)
379                                     --keep   A       C     C
380
381           working index HEAD target         working index HEAD
382           ----------------------------------------------------
383            B       B     C    D     --soft   B       B     D
384                                     --mixed  B       D     D
385                                     --hard   D       D     D
386                                     --merge  D       D     D
387                                     --keep  (disallowed)
388
389           working index HEAD target         working index HEAD
390           ----------------------------------------------------
391            B       B     C    C     --soft   B       B     C
392                                     --mixed  B       C     C
393                                     --hard   C       C     C
394                                     --merge  C       C     C
395                                     --keep   B       C     C
396
397           working index HEAD target         working index HEAD
398           ----------------------------------------------------
399            B       C     C    D     --soft   B       C     D
400                                     --mixed  B       D     D
401                                     --hard   D       D     D
402                                     --merge (disallowed)
403                                     --keep  (disallowed)
404
405           working index HEAD target         working index HEAD
406           ----------------------------------------------------
407            B       C     C    C     --soft   B       C     C
408                                     --mixed  B       C     C
409                                     --hard   C       C     C
410                                     --merge  B       C     C
411                                     --keep   B       C     C
412
413       reset --merge is meant to be used when resetting out of a conflicted
414       merge. Any mergy operation guarantees that the working tree file that
415       is involved in the merge does not have a local change with respect to
416       the index before it starts, and that it writes the result out to the
417       working tree. So if we see some difference between the index and the
418       target and also between the index and the working tree, then it means
419       that we are not resetting out from a state that a mergy operation left
420       after failing with a conflict. That is why we disallow --merge option
421       in this case.
422
423       reset --keep is meant to be used when removing some of the last commits
424       in the current branch while keeping changes in the working tree. If
425       there could be conflicts between the changes in the commit we want to
426       remove and the changes in the working tree we want to keep, the reset
427       is disallowed. That’s why it is disallowed if there are both changes
428       between the working tree and HEAD, and between HEAD and the target. To
429       be safe, it is also disallowed when there are unmerged entries.
430
431       The following tables show what happens when there are unmerged entries:
432
433           working index HEAD target         working index HEAD
434           ----------------------------------------------------
435            X       U     A    B     --soft  (disallowed)
436                                     --mixed  X       B     B
437                                     --hard   B       B     B
438                                     --merge  B       B     B
439                                     --keep  (disallowed)
440
441           working index HEAD target         working index HEAD
442           ----------------------------------------------------
443            X       U     A    A     --soft  (disallowed)
444                                     --mixed  X       A     A
445                                     --hard   A       A     A
446                                     --merge  A       A     A
447                                     --keep  (disallowed)
448
449       X means any state and U means an unmerged index.
450

GIT

452       Part of the git(1) suite
453
454
455
456Git 2.26.2                        2020-04-20                      GIT-RESET(1)
Impressum