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/>
7 .\" Source: Git 2.46.0.288.g3a7362eb9f
10 .TH "GIT\-RESET" "1" "2024-08-21" "Git 2\&.46\&.0\&.288\&.g3a7362" "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 \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\&.
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 \fB<tree\-ish>\fR\&. (It does not affect the working tree or the current branch\&.)
52 \fBgit reset <pathspec>\fR
54 \fBgit add <pathspec>\fR\&. This command is equivalent to
55 \fBgit restore [\-\-source=<tree\-ish>] \-\-staged <pathspec>\&.\&.\&.\fR\&.
58 \fBgit reset <pathspec>\fR
59 to update the index entry, you can use
61 to check the contents out of the index to the working tree\&. Alternatively, using
63 and specifying a commit with
64 \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\&.
67 \fIgit reset\fR (\-\-patch | \-p) [<tree\-ish>] [\-\-] [<pathspec>\&...\:]
69 Interactively select hunks in the difference between the index and
72 \fBHEAD\fR)\&. The chosen hunks are applied in reverse to the index\&.
77 \fBgit add \-p\fR, i\&.e\&. you can use it to selectively reset hunks\&. See the
78 \(lqInteractive Mode\(rq
81 to learn how to operate the
86 \fIgit reset\fR [<mode>] [<commit>]
88 This form resets the current branch head to
90 and possibly updates the index (resetting it to the tree of
91 \fB<commit>\fR) and the working tree depending on
92 \fB<mode>\fR\&. Before the operation,
94 is set to the tip of the current branch\&. If
96 is omitted, defaults to
97 \fB\-\-mixed\fR\&. The
99 must be one of the following:
103 Does not touch the index file or the working tree at all (but resets the head to
104 \fB<commit>\fR, just like all modes do)\&. This leaves all your changed files "Changes to be committed", as
111 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\&.
115 is specified, removed paths are marked as intent\-to\-add (see
121 Resets the index and working tree\&. Any changes to tracked files in the working tree since
123 are discarded\&. Any untracked files or directories in the way of writing any tracked files are simply deleted\&.
128 Resets the index and updates the files in the working tree that are different between
131 \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
133 and the index has unstaged changes, reset is aborted\&.
137 does something like a
138 \fBgit read\-tree \-u \-m <commit>\fR, but carries forward unmerged index entries\&.
143 Resets index entries and updates files in the working tree that are different between
146 \fBHEAD\fR\&. If a file that is different between
150 has local changes, reset is aborted\&.
153 \-\-[no\-]recurse\-submodules
155 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\&.
159 See "Reset, restore and revert" in \fBgit\fR(1) for the differences between the three commands\&.
164 Be quiet, only report errors\&.
167 \-\-refresh, \-\-no\-refresh
169 Refresh the index after a mixed reset\&. Enabled by default\&.
172 \-\-pathspec\-from\-file=<file>
174 Pathspec is passed in
176 instead of commandline args\&. If
180 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
181 \fBcore\&.quotePath\fR
183 \fBgit-config\fR(1))\&. See also
184 \fB\-\-pathspec\-file\-nul\fR
186 \fB\-\-literal\-pathspecs\fR\&.
189 \-\-pathspec\-file\-nul
192 \fB\-\-pathspec\-from\-file\fR\&. Pathspec elements are separated with NUL character and all other characters are taken literally (including newlines and quotes)\&.
197 Do not interpret any more arguments as options\&.
202 Limits the paths affected by the operation\&.
204 For more details, see the
207 \fBgitglossary\fR(7)\&.
219 $ git add frotz\&.c filfre\&.c
221 $ git reset \fB(3)\fR
222 $ 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
290 \fBgit-commit\fR(1)\&.
293 Undo a commit, making it a topic branch
300 $ git branch topic/wip \fB(1)\fR
301 $ git reset \-\-hard HEAD~3 \fB(2)\fR
302 $ git switch topic/wip \fB(3)\fR
309 r lw(\n(.lu*75u/100u).
311 You have made some commits, but realize they were premature to be in the
313 branch\&. You want to continue polishing them in a topic branch, so create
315 branch off of the current
319 Rewind the master branch to get rid of those three commits\&.
324 branch and keep working\&.
329 Undo commits permanently
336 $ git commit \&.\&.\&.
337 $ git reset \-\-hard HEAD~3 \fB(1)\fR
344 r lw(\n(.lu*75u/100u).
346 The last three commits (\fBHEAD\fR,
348 \fBHEAD~2\fR) were bad and you do not want to ever see them again\&. Do
350 do this if you have already given these commits to somebody else\&. (See the "RECOVERING FROM UPSTREAM REBASE" section in
352 for the implications of doing so\&.)
366 CONFLICT (content): Merge conflict in nitfol
367 Automatic merge failed; fix conflicts and then commit the result\&.
368 $ git reset \-\-hard \fB(2)\fR
369 $ git pull \&. topic/branch \fB(3)\fR
370 Updating from 41223\&.\&.\&. to 13134\&.\&.\&.
372 $ git reset \-\-hard ORIG_HEAD \fB(4)\fR
379 r lw(\n(.lu*75u/100u).
381 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\&.
384 "pull" has not made merge commit, so
385 \fBgit reset \-\-hard\fR
386 which is a synonym for
387 \fBgit reset \-\-hard HEAD\fR
388 clears the mess from the index file and the working tree\&.
391 Merge a topic branch into the current branch, which resulted in a fast\-forward\&.
394 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
395 \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\&.
400 Undo a merge or pull inside a dirty working tree
409 Merge made by recursive\&.
410 nitfol | 20 +++++\-\-\-\-
412 $ git reset \-\-merge ORIG_HEAD \fB(2)\fR
419 r lw(\n(.lu*75u/100u).
421 Even if you may have local modifications in your working tree, you can safely say
423 when you know that the change in the other branch does not overlap with them\&.
426 After inspecting the result of the merge, you may find that the change in the other branch is unsatisfactory\&. Running
427 \fBgit reset \-\-hard ORIG_HEAD\fR
428 will let you go back to where you were, but it will discard your local changes, which you do not want\&.
429 \fBgit reset \-\-merge\fR
430 keeps your local changes\&.
437 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\&.
443 $ git switch feature ;# you were working in "feature" branch and
444 $ work work work ;# got interrupted
445 $ git commit \-a \-m "snapshot WIP" \fB(1)\fR
448 $ git commit ;# commit with real log
450 $ git reset \-\-soft HEAD^ ;# go back to WIP state \fB(2)\fR
451 $ git reset \fB(3)\fR
459 r lw(\n(.lu*75u/100u).
461 This commit will get blown away so a throw\-away log message is OK\&.
466 commit from the commit history, and sets your working tree to the state just before you made that snapshot\&.
469 At this point the index file still has all the WIP changes you committed as
470 \fIsnapshot WIP\fR\&. This updates the index to show your WIP files as uncommitted\&.
474 \fBgit-stash\fR(1)\&.
477 Reset a single file in the index
479 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\&.
485 $ git reset \-\- frotz\&.c \fB(1)\fR
486 $ git commit \-m "Commit files in index" \fB(2)\fR
487 $ git add frotz\&.c \fB(3)\fR
494 r lw(\n(.lu*75u/100u).
496 This removes the file from the index while keeping it in the working directory\&.
499 This commits all other changes in the index\&.
502 Adds the file to the index again\&.
507 Keep changes in working tree while discarding some previous commits
509 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\&.
516 $ git switch \-c branch1
518 $ git commit \&.\&.\&. \fB(1)\fR
520 $ git switch \-c branch2 \fB(2)\fR
521 $ git reset \-\-keep start \fB(3)\fR
528 r lw(\n(.lu*75u/100u).
530 This commits your first edits in
534 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
537 \fBgit switch \-c branch2 start\fR), but nobody is perfect\&.
542 to remove the unwanted commit after you switched to
548 Split a commit apart into a sequence of commits
550 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
552 to interactively select which hunks to include into each commit, using
554 to pre\-populate the commit message\&.
560 $ git reset \-N HEAD^ \fB(1)\fR
561 $ git add \-p \fB(2)\fR
562 $ git diff \-\-cached \fB(3)\fR
563 $ git commit \-c HEAD@{1} \fB(4)\fR
565 $ git add \&.\&.\&. \fB(6)\fR
566 $ git diff \-\-cached \fB(7)\fR
567 $ git commit \&.\&.\&. \fB(8)\fR
574 r lw(\n(.lu*75u/100u).
576 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
578 are still marked so that
583 Next, we interactively select diff hunks to add using the
585 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\&.
588 Once satisfied with the hunks you want to include, you should verify what has been prepared for the first commit by using
589 \fBgit diff \-\-cached\fR\&. This shows all the changes that have been moved into the index and are about to be committed\&.
592 Next, commit the changes stored in the index\&. The
594 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
596 is a special notation for the commit that
598 used to be at prior to the original reset commit (1 change ago)\&. See
600 for more details\&. You may also use any other valid commit reference\&.
603 You can repeat steps 2\-4 multiple times to break the original code into any number of commits\&.
606 Now you\(cqve split out many of the changes into their own commits, and might no longer use the patch mode of
607 \fBgit add\fR, in order to select all remaining uncommitted changes\&.
610 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\&.
613 And finally create the final commit\&.
619 The tables below show what happens when running:
625 git reset \-\-option target
631 to reset the \fBHEAD\fR to another commit (\fBtarget\fR) with the different reset options depending on the state of the files\&.
633 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)\&.
639 working index HEAD target working index HEAD
640 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
641 A B C D \-\-soft A B D
644 \-\-merge (disallowed)
645 \-\-keep (disallowed)
655 working index HEAD target working index HEAD
656 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
657 A B C C \-\-soft A B C
660 \-\-merge (disallowed)
671 working index HEAD target working index HEAD
672 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
673 B B C D \-\-soft B B D
677 \-\-keep (disallowed)
687 working index HEAD target working index HEAD
688 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
689 B B C C \-\-soft B B C
703 working index HEAD target working index HEAD
704 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
705 B C C D \-\-soft B C D
708 \-\-merge (disallowed)
709 \-\-keep (disallowed)
719 working index HEAD target working index HEAD
720 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
721 B C C C \-\-soft B C C
731 \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\&.
733 \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\&.
735 The following tables show what happens when there are unmerged entries:
741 working index HEAD target working index HEAD
742 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
743 X U A B \-\-soft (disallowed)
747 \-\-keep (disallowed)
757 working index HEAD target working index HEAD
758 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
759 X U A A \-\-soft (disallowed)
763 \-\-keep (disallowed)
769 \fBX\fR means any state and \fBU\fR means an unmerged index\&.
772 Part of the \fBgit\fR(1) suite