3 .\" Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
4 .\" Generator: DocBook XSL Stylesheets v1.79.2 <http://docbook.sf.net/>
10 .TH "GIT\-RESET" "1" "2025-01-10" "Git 2\&.48\&.0" "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>]
42 In the first three forms, copy entries from \fI<tree\-ish>\fR to the index\&. In the last form, set the current branch head (\fBHEAD\fR) to \fI<commit>\fR, optionally modifying index and working tree to match\&. The \fI<tree\-ish>\fR/\fI<commit>\fR defaults to \fBHEAD\fR in all forms\&.
44 \fIgit reset\fR [\-q] [<tree\-ish>] [\-\-] <pathspec>\&...\:, \fIgit reset\fR [\-q] [\-\-pathspec\-from\-file=<file> [\-\-pathspec\-file\-nul]] [<tree\-ish>]
46 These forms reset the index entries for all paths that match the
49 \fI<tree\-ish>\fR\&. (It does not affect the working tree or the current branch\&.)
58 \fI<pathspec>\fR\&. This command is equivalent to
61 [\fB\-\-source=\fR\fI<tree\-ish>\fR]
63 \fI<pathspec>\fR\&.\&.\&.\&.
69 to update the index entry, you can use
71 to check the contents out of the index to the working tree\&. Alternatively, using
73 and specifying a commit with
74 \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\&.
77 \fIgit reset\fR (\-\-patch | \-p) [<tree\-ish>] [\-\-] [<pathspec>\&...\:]
79 Interactively select hunks in the difference between the index and
82 \fBHEAD\fR)\&. The chosen hunks are applied in reverse to the index\&.
91 \fB\-p\fR, i\&.e\&. you can use it to selectively reset hunks\&. See the
92 \(lqInteractive Mode\(rq
95 to learn how to operate the
100 \fIgit reset\fR [<mode>] [<commit>]
102 This form resets the current branch head to
104 and possibly updates the index (resetting it to the tree of
105 \fI<commit>\fR) and the working tree depending on
106 \fI<mode>\fR\&. Before the operation,
108 is set to the tip of the current branch\&. If
110 is omitted, defaults to
111 \fB\-\-mixed\fR\&. The
113 must be one of the following:
117 Does not touch the index file or the working tree at all (but resets the head to
118 \fI<commit>\fR, just like all modes do)\&. This leaves all your changed files "Changes to be committed", as
126 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\&.
130 is specified, removed paths are marked as intent\-to\-add (see
136 Resets the index and working tree\&. Any changes to tracked files in the working tree since
138 are discarded\&. Any untracked files or directories in the way of writing any tracked files are simply deleted\&.
143 Resets the index and updates the files in the working tree that are different between
146 \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
148 and the index has unstaged changes, reset is aborted\&.
152 does something like a
157 \fI<commit>\fR, but carries forward unmerged index entries\&.
162 Resets index entries and updates files in the working tree that are different between
165 \fBHEAD\fR\&. If a file that is different between
169 has local changes, reset is aborted\&.
172 \-\-[no\-]recurse\-submodules
174 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\&.
178 See "Reset, restore and revert" in \fBgit\fR(1) for the differences between the three commands\&.
183 Be quiet, only report errors\&.
186 \-\-refresh, \-\-no\-refresh
188 Refresh the index after a mixed reset\&. Enabled by default\&.
191 \-\-pathspec\-from\-file=<file>
193 Pathspec is passed in
195 instead of commandline args\&. If
199 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
200 \fBcore\&.quotePath\fR
202 \fBgit-config\fR(1))\&. See also
203 \fB\-\-pathspec\-file\-nul\fR
205 \fB\-\-literal\-pathspecs\fR\&.
208 \-\-pathspec\-file\-nul
211 \fB\-\-pathspec\-from\-file\fR\&. Pathspec elements are separated with NUL character and all other characters are taken literally (including newlines and quotes)\&.
216 Do not interpret any more arguments as options\&.
221 Limits the paths affected by the operation\&.
223 For more details, see the
226 \fBgitglossary\fR(7)\&.
238 $ git add frotz\&.c filfre\&.c
240 $ git reset \fB(3)\fR
241 $ git pull git://info\&.example\&.com/ nitfol \fB(4)\fR
248 r lw(\n(.lu*75u/100u).
250 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
252 \fBdiff\fR, because you plan to work on other files and changes with these files are distracting\&.
255 Somebody asks you to pull, and the changes sound worthy of merging\&.
258 However, you already dirtied the index (i\&.e\&. your index does not match the
260 commit)\&. But you know the pull you are going to make does not affect
263 \fBfilfre\&.c\fR, so you revert the index changes for these two files\&. Your changes in working tree remain there\&.
266 Then you can pull and merge, leaving
270 changes still in the working tree\&.
275 Undo a commit and redo
282 $ git commit \&.\&.\&.
283 $ git reset \-\-soft HEAD^ \fB(1)\fR
285 $ git commit \-a \-c ORIG_HEAD \fB(3)\fR
293 r lw(\n(.lu*75u/100u).
295 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"\&.
298 Make corrections to working tree files\&.
301 "reset" copies the old head to \&.\fBgit/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
309 \fBgit-commit\fR(1)\&.
312 Undo a commit, making it a topic branch
319 $ git branch topic/wip \fB(1)\fR
320 $ git reset \-\-hard HEAD~3 \fB(2)\fR
321 $ git switch topic/wip \fB(3)\fR
328 r lw(\n(.lu*75u/100u).
330 You have made some commits, but realize they were premature to be in the
332 branch\&. You want to continue polishing them in a topic branch, so create
334 branch off of the current
338 Rewind the master branch to get rid of those three commits\&.
343 branch and keep working\&.
348 Undo commits permanently
355 $ git commit \&.\&.\&.
356 $ git reset \-\-hard HEAD~3 \fB(1)\fR
363 r lw(\n(.lu*75u/100u).
365 The last three commits (\fBHEAD\fR,
367 \fBHEAD~2\fR) were bad and you do not want to ever see them again\&. Do
369 do this if you have already given these commits to somebody else\&. (See the "RECOVERING FROM UPSTREAM REBASE" section in
371 for the implications of doing so\&.)
385 CONFLICT (content): Merge conflict in nitfol
386 Automatic merge failed; fix conflicts and then commit the result\&.
387 $ git reset \-\-hard \fB(2)\fR
388 $ git pull \&. topic/branch \fB(3)\fR
389 Updating from 41223\&.\&.\&. to 13134\&.\&.\&.
391 $ git reset \-\-hard ORIG_HEAD \fB(4)\fR
398 r lw(\n(.lu*75u/100u).
400 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\&.
403 "pull" has not made merge commit, so
407 which is a synonym for
412 clears the mess from the index file and the working tree\&.
415 Merge a topic branch into the current branch, which resulted in a fast\-forward\&.
418 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
419 \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\&.
424 Undo a merge or pull inside a dirty working tree
433 Merge made by recursive\&.
434 nitfol | 20 +++++\-\-\-\-
436 $ git reset \-\-merge ORIG_HEAD \fB(2)\fR
443 r lw(\n(.lu*75u/100u).
445 Even if you may have local modifications in your working tree, you can safely say
448 when you know that the change in the other branch does not overlap with them\&.
451 After inspecting the result of the merge, you may find that the change in the other branch is unsatisfactory\&. Running
456 will let you go back to where you were, but it will discard your local changes, which you do not want\&.
460 keeps your local changes\&.
467 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\&.
473 $ git switch feature ;# you were working in "feature" branch and
474 $ work work work ;# got interrupted
475 $ git commit \-a \-m "snapshot WIP" \fB(1)\fR
478 $ git commit ;# commit with real log
480 $ git reset \-\-soft HEAD^ ;# go back to WIP state \fB(2)\fR
481 $ git reset \fB(3)\fR
489 r lw(\n(.lu*75u/100u).
491 This commit will get blown away so a throw\-away log message is OK\&.
496 commit from the commit history, and sets your working tree to the state just before you made that snapshot\&.
499 At this point the index file still has all the WIP changes you committed as
500 \fIsnapshot WIP\fR\&. This updates the index to show your WIP files as uncommitted\&.
504 \fBgit-stash\fR(1)\&.
507 Reset a single file in the index
509 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\&.
515 $ git reset \-\- frotz\&.c \fB(1)\fR
516 $ git commit \-m "Commit files in index" \fB(2)\fR
517 $ git add frotz\&.c \fB(3)\fR
524 r lw(\n(.lu*75u/100u).
526 This removes the file from the index while keeping it in the working directory\&.
529 This commits all other changes in the index\&.
532 Adds the file to the index again\&.
537 Keep changes in working tree while discarding some previous commits
539 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\&.
546 $ git switch \-c branch1
548 $ git commit \&.\&.\&. \fB(1)\fR
550 $ git switch \-c branch2 \fB(2)\fR
551 $ git reset \-\-keep start \fB(3)\fR
558 r lw(\n(.lu*75u/100u).
560 This commits your first edits in
564 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
571 \fBstart\fR), but nobody is perfect\&.
577 to remove the unwanted commit after you switched to
583 Split a commit apart into a sequence of commits
585 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
589 to interactively select which hunks to include into each commit, using
593 to pre\-populate the commit message\&.
599 $ git reset \-N HEAD^ \fB(1)\fR
600 $ git add \-p \fB(2)\fR
601 $ git diff \-\-cached \fB(3)\fR
602 $ git commit \-c HEAD@{1} \fB(4)\fR
604 $ git add \&.\&.\&. \fB(6)\fR
605 $ git diff \-\-cached \fB(7)\fR
606 $ git commit \&.\&.\&. \fB(8)\fR
613 r lw(\n(.lu*75u/100u).
615 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
617 are still marked so that
624 Next, we interactively select diff hunks to add using the
628 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\&.
631 Once satisfied with the hunks you want to include, you should verify what has been prepared for the first commit by using
634 \fB\-\-cached\fR\&. This shows all the changes that have been moved into the index and are about to be committed\&.
637 Next, commit the changes stored in the index\&. The
639 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
640 \fBHEAD@\fR{1} is a special notation for the commit that
642 used to be at prior to the original reset commit (1 change ago)\&. See
644 for more details\&. You may also use any other valid commit reference\&.
647 You can repeat steps 2\-4 multiple times to break the original code into any number of commits\&.
650 Now you\(cqve split out many of the changes into their own commits, and might no longer use the patch mode of
652 \fBadd\fR, in order to select all remaining uncommitted changes\&.
655 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\&.
658 And finally create the final commit\&.
664 The tables below show what happens when running:
670 git reset \-\-option target
676 to reset the \fBHEAD\fR to another commit (\fBtarget\fR) with the different reset options depending on the state of the files\&.
678 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\fR \fBreset\fR \fB\-\-soft\fR \fBtarget\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)\&.
684 working index HEAD target working index HEAD
685 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
686 A B C D \-\-soft A B D
689 \-\-merge (disallowed)
690 \-\-keep (disallowed)
700 working index HEAD target working index HEAD
701 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
702 A B C C \-\-soft A B C
705 \-\-merge (disallowed)
716 working index HEAD target working index HEAD
717 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
718 B B C D \-\-soft B B D
722 \-\-keep (disallowed)
732 working index HEAD target working index HEAD
733 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
734 B B C C \-\-soft B B C
748 working index HEAD target working index HEAD
749 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
750 B C C D \-\-soft B C D
753 \-\-merge (disallowed)
754 \-\-keep (disallowed)
764 working index HEAD target working index HEAD
765 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
766 B C C C \-\-soft B C C
776 \fBreset\fR \fB\-\-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\&.
778 \fBreset\fR \fB\-\-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\&.
780 The following tables show what happens when there are unmerged entries:
786 working index HEAD target working index HEAD
787 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
788 X U A B \-\-soft (disallowed)
792 \-\-keep (disallowed)
802 working index HEAD target working index HEAD
803 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
804 X U A A \-\-soft (disallowed)
808 \-\-keep (disallowed)
814 \fBX\fR means any state and \fBU\fR means an unmerged index\&.
817 Part of the \fBgit\fR(1) suite