3 .\" Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
7 .\" Source: Git 2.44.0.rc2
10 .TH "GIT\-RESET" "1" "2024\-02\-19" "Git 2\&.44\&.0\&.rc2" "Git Manual"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 git-reset \- Reset current HEAD to the specified state
35 \fIgit reset\fR [\-q] [<tree\-ish>] [\-\-] <pathspec>\&...
36 \fIgit reset\fR [\-q] [\-\-pathspec\-from\-file=<file> [\-\-pathspec\-file\-nul]] [<tree\-ish>]
37 \fIgit reset\fR (\-\-patch | \-p) [<tree\-ish>] [\-\-] [<pathspec>\&...]
38 \fIgit reset\fR [\-\-soft | \-\-mixed [\-N] | \-\-hard | \-\-merge | \-\-keep] [\-q] [<commit>]
43 In the first three forms, copy entries from \fB<tree\-ish>\fR to the index\&. In the last form, set the current branch head (\fBHEAD\fR) to \fB<commit>\fR, optionally modifying index and working tree to match\&. The \fB<tree\-ish>\fR/\fB<commit>\fR defaults to \fBHEAD\fR in all forms\&.
45 \fIgit reset\fR [\-q] [<tree\-ish>] [\-\-] <pathspec>\&..., \fIgit reset\fR [\-q] [\-\-pathspec\-from\-file=<file> [\-\-pathspec\-file\-nul]] [<tree\-ish>]
47 These forms reset the index entries for all paths that match the
50 \fB<tree\-ish>\fR\&. (It does not affect the working tree or the current branch\&.)
53 \fBgit reset <pathspec>\fR
55 \fBgit add <pathspec>\fR\&. This command is equivalent to
56 \fBgit restore [\-\-source=<tree\-ish>] \-\-staged <pathspec>\&.\&.\&.\fR\&.
59 \fBgit reset <pathspec>\fR
60 to update the index entry, you can use
62 to check the contents out of the index to the working tree\&. Alternatively, using
64 and specifying a commit with
65 \fB\-\-source\fR, you can copy the contents of a path out of a commit to the index and to the working tree in one go\&.
68 \fIgit reset\fR (\-\-patch | \-p) [<tree\-ish>] [\-\-] [<pathspec>\&...]
70 Interactively select hunks in the difference between the index and
73 \fBHEAD\fR)\&. The chosen hunks are applied in reverse to the index\&.
78 \fBgit add \-p\fR, i\&.e\&. you can use it to selectively reset hunks\&. See the \(lqInteractive Mode\(rq section of
80 to learn how to operate the
85 \fIgit reset\fR [<mode>] [<commit>]
87 This form resets the current branch head to
89 and possibly updates the index (resetting it to the tree of
90 \fB<commit>\fR) and the working tree depending on
91 \fB<mode>\fR\&. Before the operation,
93 is set to the tip of the current branch\&. If
95 is omitted, defaults to
96 \fB\-\-mixed\fR\&. The
98 must be one of the following:
102 Does not touch the index file or the working tree at all (but resets the head to
103 \fB<commit>\fR, just like all modes do)\&. This leaves all your changed files "Changes to be committed", as
110 Resets the index but not the working tree (i\&.e\&., the changed files are preserved but not marked for commit) and reports what has not been updated\&. This is the default action\&.
114 is specified, removed paths are marked as intent\-to\-add (see
120 Resets the index and working tree\&. Any changes to tracked files in the working tree since
122 are discarded\&. Any untracked files or directories in the way of writing any tracked files are simply deleted\&.
127 Resets the index and updates the files in the working tree that are different between
130 \fBHEAD\fR, but keeps those which are different between the index and working tree (i\&.e\&. which have changes which have not been added)\&. If a file that is different between
132 and the index has unstaged changes, reset is aborted\&.
136 does something like a
137 \fBgit read\-tree \-u \-m <commit>\fR, but carries forward unmerged index entries\&.
142 Resets index entries and updates files in the working tree that are different between
145 \fBHEAD\fR\&. If a file that is different between
149 has local changes, reset is aborted\&.
152 \-\-[no\-]recurse\-submodules
154 When the working tree is updated, using \-\-recurse\-submodules will also recursively reset the working tree of all active submodules according to the commit recorded in the superproject, also setting the submodules\*(Aq HEAD to be detached at that commit\&.
158 See "Reset, restore and revert" in \fBgit\fR(1) for the differences between the three commands\&.
163 Be quiet, only report errors\&.
166 \-\-refresh, \-\-no\-refresh
168 Refresh the index after a mixed reset\&. Enabled by default\&.
171 \-\-pathspec\-from\-file=<file>
173 Pathspec is passed in
175 instead of commandline args\&. If
179 then standard input is used\&. Pathspec elements are separated by LF or CR/LF\&. Pathspec elements can be quoted as explained for the configuration variable
180 \fBcore\&.quotePath\fR
182 \fBgit-config\fR(1))\&. See also
183 \fB\-\-pathspec\-file\-nul\fR
185 \fB\-\-literal\-pathspecs\fR\&.
188 \-\-pathspec\-file\-nul
191 \fB\-\-pathspec\-from\-file\fR\&. Pathspec elements are separated with NUL character and all other characters are taken literally (including newlines and quotes)\&.
196 Do not interpret any more arguments as options\&.
201 Limits the paths affected by the operation\&.
203 For more details, see the
206 \fBgitglossary\fR(7)\&.
218 $ git add frotz\&.c filfre\&.c
220 $ git reset \fB(3)\fR
221 $ git pull git://info\&.example\&.com/ nitfol \fB(4)\fR
229 r lw(\n(.lu*75u/100u).
231 You are happily working on something, and find the changes in these files are in good order\&. You do not want to see them when you run
232 \fBgit diff\fR, because you plan to work on other files and changes with these files are distracting\&.
235 Somebody asks you to pull, and the changes sound worthy of merging\&.
238 However, you already dirtied the index (i\&.e\&. your index does not match the
240 commit)\&. But you know the pull you are going to make does not affect
243 \fBfilfre\&.c\fR, so you revert the index changes for these two files\&. Your changes in working tree remain there\&.
246 Then you can pull and merge, leaving
250 changes still in the working tree\&.
255 Undo a commit and redo
262 $ git commit \&.\&.\&.
263 $ git reset \-\-soft HEAD^ \fB(1)\fR
265 $ git commit \-a \-c ORIG_HEAD \fB(3)\fR
273 r lw(\n(.lu*75u/100u).
275 This is most often done when you remembered what you just committed is incomplete, or you misspelled your commit message, or both\&. Leaves working tree as it was before "reset"\&.
278 Make corrections to working tree files\&.
281 "reset" copies the old head to
282 \fB\&.git/ORIG_HEAD\fR; redo the commit by starting with its log message\&. If you do not need to edit the message further, you can give
289 \fBgit-commit\fR(1)\&.
294 Undo a commit, making it a topic branch
301 $ git branch topic/wip \fB(1)\fR
302 $ git reset \-\-hard HEAD~3 \fB(2)\fR
303 $ git switch topic/wip \fB(3)\fR
311 r lw(\n(.lu*75u/100u).
313 You have made some commits, but realize they were premature to be in the
315 branch\&. You want to continue polishing them in a topic branch, so create
317 branch off of the current
321 Rewind the master branch to get rid of those three commits\&.
326 branch and keep working\&.
331 Undo commits permanently
338 $ git commit \&.\&.\&.
339 $ git reset \-\-hard HEAD~3 \fB(1)\fR
347 r lw(\n(.lu*75u/100u).
349 The last three commits (\fBHEAD\fR,
351 \fBHEAD~2\fR) were bad and you do not want to ever see them again\&. Do
353 do this if you have already given these commits to somebody else\&. (See the "RECOVERING FROM UPSTREAM REBASE" section in
355 for the implications of doing so\&.)
369 CONFLICT (content): Merge conflict in nitfol
370 Automatic merge failed; fix conflicts and then commit the result\&.
371 $ git reset \-\-hard \fB(2)\fR
372 $ git pull \&. topic/branch \fB(3)\fR
373 Updating from 41223\&.\&.\&. to 13134\&.\&.\&.
375 $ git reset \-\-hard ORIG_HEAD \fB(4)\fR
383 r lw(\n(.lu*75u/100u).
385 Try to update from the upstream resulted in a lot of conflicts; you were not ready to spend a lot of time merging right now, so you decide to do that later\&.
388 "pull" has not made merge commit, so
389 \fBgit reset \-\-hard\fR
390 which is a synonym for
391 \fBgit reset \-\-hard HEAD\fR
392 clears the mess from the index file and the working tree\&.
395 Merge a topic branch into the current branch, which resulted in a fast\-forward\&.
398 But you decided that the topic branch is not ready for public consumption yet\&. "pull" or "merge" always leaves the original tip of the current branch in
399 \fBORIG_HEAD\fR, so resetting hard to it brings your index file and the working tree back to that state, and resets the tip of the branch to that commit\&.
404 Undo a merge or pull inside a dirty working tree
413 Merge made by recursive\&.
414 nitfol | 20 +++++\-\-\-\-
416 $ git reset \-\-merge ORIG_HEAD \fB(2)\fR
424 r lw(\n(.lu*75u/100u).
426 Even if you may have local modifications in your working tree, you can safely say
428 when you know that the change in the other branch does not overlap with them\&.
431 After inspecting the result of the merge, you may find that the change in the other branch is unsatisfactory\&. Running
432 \fBgit reset \-\-hard ORIG_HEAD\fR
433 will let you go back to where you were, but it will discard your local changes, which you do not want\&.
434 \fBgit reset \-\-merge\fR
435 keeps your local changes\&.
442 Suppose you are interrupted by an urgent fix request while you are in the middle of a large change\&. The files in your working tree are not in any shape to be committed yet, but you need to get to the other branch for a quick bugfix\&.
448 $ git switch feature ;# you were working in "feature" branch and
449 $ work work work ;# got interrupted
450 $ git commit \-a \-m "snapshot WIP" \fB(1)\fR
453 $ git commit ;# commit with real log
455 $ git reset \-\-soft HEAD^ ;# go back to WIP state \fB(2)\fR
456 $ git reset \fB(3)\fR
464 r lw(\n(.lu*75u/100u).
466 This commit will get blown away so a throw\-away log message is OK\&.
471 commit from the commit history, and sets your working tree to the state just before you made that snapshot\&.
474 At this point the index file still has all the WIP changes you committed as
475 \fIsnapshot WIP\fR\&. This updates the index to show your WIP files as uncommitted\&.
478 \fBgit-stash\fR(1)\&.
483 Reset a single file in the index
485 Suppose you have added a file to your index, but later decide you do not want to add it to your commit\&. You can remove the file from the index while keeping your changes with git reset\&.
491 $ git reset \-\- frotz\&.c \fB(1)\fR
492 $ git commit \-m "Commit files in index" \fB(2)\fR
493 $ git add frotz\&.c \fB(3)\fR
501 r lw(\n(.lu*75u/100u).
503 This removes the file from the index while keeping it in the working directory\&.
506 This commits all other changes in the index\&.
509 Adds the file to the index again\&.
514 Keep changes in working tree while discarding some previous commits
516 Suppose you are working on something and you commit it, and then you continue working a bit more, but now you think that what you have in your working tree should be in another branch that has nothing to do with what you committed previously\&. You can start a new branch and reset it while keeping the changes in your working tree\&.
523 $ git switch \-c branch1
525 $ git commit \&.\&.\&. \fB(1)\fR
527 $ git switch \-c branch2 \fB(2)\fR
528 $ git reset \-\-keep start \fB(3)\fR
536 r lw(\n(.lu*75u/100u).
538 This commits your first edits in
542 In the ideal world, you could have realized that the earlier commit did not belong to the new topic when you created and switched to
545 \fBgit switch \-c branch2 start\fR), but nobody is perfect\&.
550 to remove the unwanted commit after you switched to
556 Split a commit apart into a sequence of commits
558 Suppose that you have created lots of logically separate changes and committed them together\&. Then, later you decide that it might be better to have each logical chunk associated with its own commit\&. You can use git reset to rewind history without changing the contents of your local files, and then successively use
560 to interactively select which hunks to include into each commit, using
562 to pre\-populate the commit message\&.
568 $ git reset \-N HEAD^ \fB(1)\fR
569 $ git add \-p \fB(2)\fR
570 $ git diff \-\-cached \fB(3)\fR
571 $ git commit \-c HEAD@{1} \fB(4)\fR
573 $ git add \&.\&.\&. \fB(6)\fR
574 $ git diff \-\-cached \fB(7)\fR
575 $ git commit \&.\&.\&. \fB(8)\fR
583 r lw(\n(.lu*75u/100u).
585 First, reset the history back one commit so that we remove the original commit, but leave the working tree with all the changes\&. The \-N ensures that any new files added with
587 are still marked so that
592 Next, we interactively select diff hunks to add using the
594 facility\&. This will ask you about each diff hunk in sequence and you can use simple commands such as "yes, include this", "No don\(cqt include this" or even the very powerful "edit" facility\&.
597 Once satisfied with the hunks you want to include, you should verify what has been prepared for the first commit by using
598 \fBgit diff \-\-cached\fR\&. This shows all the changes that have been moved into the index and are about to be committed\&.
601 Next, commit the changes stored in the index\&. The
603 option specifies to pre\-populate the commit message from the original message that you started with in the first commit\&. This is helpful to avoid retyping it\&. The
605 is a special notation for the commit that
607 used to be at prior to the original reset commit (1 change ago)\&. See
609 for more details\&. You may also use any other valid commit reference\&.
612 You can repeat steps 2\-4 multiple times to break the original code into any number of commits\&.
615 Now you\(cqve split out many of the changes into their own commits, and might no longer use the patch mode of
616 \fBgit add\fR, in order to select all remaining uncommitted changes\&.
619 Once again, check to verify that you\(cqve included what you want to\&. You may also wish to verify that git diff doesn\(cqt show any remaining changes to be committed later\&.
622 And finally create the final commit\&.
628 The tables below show what happens when running:
634 git reset \-\-option target
641 to reset the \fBHEAD\fR to another commit (\fBtarget\fR) with the different reset options depending on the state of the files\&.
643 In these tables, \fBA\fR, \fBB\fR, \fBC\fR and \fBD\fR are some different states of a file\&. For example, the first line of the first table means that if a file is in state \fBA\fR in the working tree, in state \fBB\fR in the index, in state \fBC\fR in \fBHEAD\fR and in state \fBD\fR in the target, then \fBgit reset \-\-soft target\fR will leave the file in the working tree in state \fBA\fR and in the index in state \fBB\fR\&. It resets (i\&.e\&. moves) the \fBHEAD\fR (i\&.e\&. the tip of the current branch, if you are on one) to \fBtarget\fR (which has the file in state \fBD\fR)\&.
649 working index HEAD target working index HEAD
650 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
651 A B C D \-\-soft A B D
654 \-\-merge (disallowed)
655 \-\-keep (disallowed)
665 working index HEAD target working index HEAD
666 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
667 A B C C \-\-soft A B C
670 \-\-merge (disallowed)
681 working index HEAD target working index HEAD
682 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
683 B B C D \-\-soft B B D
687 \-\-keep (disallowed)
697 working index HEAD target working index HEAD
698 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
699 B B C C \-\-soft B B C
713 working index HEAD target working index HEAD
714 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
715 B C C D \-\-soft B C D
718 \-\-merge (disallowed)
719 \-\-keep (disallowed)
729 working index HEAD target working index HEAD
730 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
731 B C C C \-\-soft B C C
741 \fBreset \-\-merge\fR is meant to be used when resetting out of a conflicted merge\&. Any mergy operation guarantees that the working tree file that is involved in the merge does not have a local change with respect to the index before it starts, and that it writes the result out to the working tree\&. So if we see some difference between the index and the target and also between the index and the working tree, then it means that we are not resetting out from a state that a mergy operation left after failing with a conflict\&. That is why we disallow \fB\-\-merge\fR option in this case\&.
743 \fBreset \-\-keep\fR is meant to be used when removing some of the last commits in the current branch while keeping changes in the working tree\&. If there could be conflicts between the changes in the commit we want to remove and the changes in the working tree we want to keep, the reset is disallowed\&. That\(cqs why it is disallowed if there are both changes between the working tree and \fBHEAD\fR, and between \fBHEAD\fR and the target\&. To be safe, it is also disallowed when there are unmerged entries\&.
745 The following tables show what happens when there are unmerged entries:
751 working index HEAD target working index HEAD
752 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
753 X U A B \-\-soft (disallowed)
757 \-\-keep (disallowed)
767 working index HEAD target working index HEAD
768 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
769 X U A A \-\-soft (disallowed)
773 \-\-keep (disallowed)
779 \fBX\fR means any state and \fBU\fR means an unmerged index\&.
782 Part of the \fBgit\fR(1) suite