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>] [--] <paths>...
10       git reset (--patch | -p) [<tree-ish>] [--] [<paths>...]
11       git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]
12
13

DESCRIPTION

15       In the first and second form, copy entries from <tree-ish> to the
16       index. In the third form, set the current branch head (HEAD) to
17       <commit>, 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>] [--] <paths>...
21           This form resets the index entries for all <paths> to their state
22           at <tree-ish>. (It does not affect the working tree or the current
23           branch.)
24
25           This means that git reset <paths> is the opposite of git add
26           <paths>. This command is equivalent to git restore
27           [--source=<tree-ish>] --staged <paths>....
28
29           After running git reset <paths> to update the index entry, you can
30           use git-restore(1) to check the contents out of the index to the
31           working tree. Alternatively, using git-restore(1) and specifying a
32           commit with --source, you can copy the contents of a path out of a
33           commit to the index and to the working tree in one go.
34
35       git reset (--patch | -p) [<tree-ish>] [--] [<paths>...]
36           Interactively select hunks in the difference between the index and
37           <tree-ish> (defaults to HEAD). The chosen hunks are applied in
38           reverse to the index.
39
40           This means that git reset -p is the opposite of git add -p, i.e.
41           you can use it to selectively reset hunks. See the “Interactive
42           Mode” section of git-add(1) to learn how to operate the --patch
43           mode.
44
45       git reset [<mode>] [<commit>]
46           This form resets the current branch head to <commit> and possibly
47           updates the index (resetting it to the tree of <commit>) and the
48           working tree depending on <mode>. If <mode> is omitted, defaults to
49           --mixed. The <mode> must be one of the following:
50
51           --soft
52               Does not touch the index file or the working tree at all (but
53               resets the head to <commit>, just like all modes do). This
54               leaves all your changed files "Changes to be committed", as git
55               status would put it.
56
57           --mixed
58               Resets the index but not the working tree (i.e., the changed
59               files are preserved but not marked for commit) and reports what
60               has not been updated. This is the default action.
61
62               If -N is specified, removed paths are marked as intent-to-add
63               (see git-add(1)).
64
65           --hard
66               Resets the index and working tree. Any changes to tracked files
67               in the working tree since <commit> are discarded.
68
69           --merge
70               Resets the index and updates the files in the working tree that
71               are different between <commit> and HEAD, but keeps those which
72               are different between the index and working tree (i.e. which
73               have changes which have not been added). If a file that is
74               different between <commit> and the index has unstaged changes,
75               reset is aborted.
76
77               In other words, --merge does something like a git read-tree -u
78               -m <commit>, but carries forward unmerged index entries.
79
80           --keep
81               Resets index entries and updates files in the working tree that
82               are different between <commit> and HEAD. If a file that is
83               different between <commit> and HEAD has local changes, reset is
84               aborted.
85
86       See "Reset, restore and revert" in git(1) for the differences between
87       the three commands.
88

OPTIONS

90       -q, --quiet, --no-quiet
91           Be quiet, only report errors. The default behavior is set by the
92           reset.quiet config option.  --quiet and --no-quiet will override
93           the default behavior.
94

EXAMPLES

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

DISCUSSION

314       The tables below show what happens when running:
315
316           git reset --option target
317
318
319       to reset the HEAD to another commit (target) with the different reset
320       options depending on the state of the files.
321
322       In these tables, A, B, C and D are some different states of a file. For
323       example, the first line of the first table means that if a file is in
324       state A in the working tree, in state B in the index, in state C in
325       HEAD and in state D in the target, then git reset --soft target will
326       leave the file in the working tree in state A and in the index in state
327       B. It resets (i.e. moves) the HEAD (i.e. the tip of the current branch,
328       if you are on one) to target (which has the file in state D).
329
330           working index HEAD target         working index HEAD
331           ----------------------------------------------------
332            A       B     C    D     --soft   A       B     D
333                                     --mixed  A       D     D
334                                     --hard   D       D     D
335                                     --merge (disallowed)
336                                     --keep  (disallowed)
337
338           working index HEAD target         working index HEAD
339           ----------------------------------------------------
340            A       B     C    C     --soft   A       B     C
341                                     --mixed  A       C     C
342                                     --hard   C       C     C
343                                     --merge (disallowed)
344                                     --keep   A       C     C
345
346           working index HEAD target         working index HEAD
347           ----------------------------------------------------
348            B       B     C    D     --soft   B       B     D
349                                     --mixed  B       D     D
350                                     --hard   D       D     D
351                                     --merge  D       D     D
352                                     --keep  (disallowed)
353
354           working index HEAD target         working index HEAD
355           ----------------------------------------------------
356            B       B     C    C     --soft   B       B     C
357                                     --mixed  B       C     C
358                                     --hard   C       C     C
359                                     --merge  C       C     C
360                                     --keep   B       C     C
361
362           working index HEAD target         working index HEAD
363           ----------------------------------------------------
364            B       C     C    D     --soft   B       C     D
365                                     --mixed  B       D     D
366                                     --hard   D       D     D
367                                     --merge (disallowed)
368                                     --keep  (disallowed)
369
370           working index HEAD target         working index HEAD
371           ----------------------------------------------------
372            B       C     C    C     --soft   B       C     C
373                                     --mixed  B       C     C
374                                     --hard   C       C     C
375                                     --merge  B       C     C
376                                     --keep   B       C     C
377
378       reset --merge is meant to be used when resetting out of a conflicted
379       merge. Any mergy operation guarantees that the working tree file that
380       is involved in the merge does not have a local change with respect to
381       the index before it starts, and that it writes the result out to the
382       working tree. So if we see some difference between the index and the
383       target and also between the index and the working tree, then it means
384       that we are not resetting out from a state that a mergy operation left
385       after failing with a conflict. That is why we disallow --merge option
386       in this case.
387
388       reset --keep is meant to be used when removing some of the last commits
389       in the current branch while keeping changes in the working tree. If
390       there could be conflicts between the changes in the commit we want to
391       remove and the changes in the working tree we want to keep, the reset
392       is disallowed. That’s why it is disallowed if there are both changes
393       between the working tree and HEAD, and between HEAD and the target. To
394       be safe, it is also disallowed when there are unmerged entries.
395
396       The following tables show what happens when there are unmerged entries:
397
398           working index HEAD target         working index HEAD
399           ----------------------------------------------------
400            X       U     A    B     --soft  (disallowed)
401                                     --mixed  X       B     B
402                                     --hard   B       B     B
403                                     --merge  B       B     B
404                                     --keep  (disallowed)
405
406           working index HEAD target         working index HEAD
407           ----------------------------------------------------
408            X       U     A    A     --soft  (disallowed)
409                                     --mixed  X       A     A
410                                     --hard   A       A     A
411                                     --merge  A       A     A
412                                     --keep  (disallowed)
413
414       X means any state and U means an unmerged index.
415

GIT

417       Part of the git(1) suite
418
419
420
421Git 2.24.1                        12/10/2019                      GIT-RESET(1)
Impressum