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

OPTIONS

89       -q, --quiet
90           Be quiet, only report errors.
91

EXAMPLES

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

DISCUSSION

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

GIT

414       Part of the git(1) suite
415
416
417
418Git 2.18.1                        05/14/2019                      GIT-RESET(1)
Impressum