58d9b4c062d6d6d09f10fd2fad1edabddd961d92
   1git-reset(1)
   2============
   3
   4NAME
   5----
   6git-reset - Reset current HEAD to the specified state
   7
   8SYNOPSIS
   9--------
  10[verse]
  11'git reset' [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]
  12'git reset' [-q] [<commit>] [--] <paths>...
  13'git reset' --patch [<commit>] [--] [<paths>...]
  14
  15DESCRIPTION
  16-----------
  17Sets the current head to the specified commit and optionally resets the
  18index and working tree to match.
  19
  20This command is useful if you notice some small error in a recent
  21commit (or set of commits) and want to redo that part without showing
  22the undo in the history.
  23
  24If you want to undo a commit other than the latest on a branch,
  25linkgit:git-revert[1] is your friend.
  26
  27The second and third forms with 'paths' and/or --patch are used to
  28revert selected paths in the index from a given commit, without moving
  29HEAD.
  30
  31
  32OPTIONS
  33-------
  34--mixed::
  35        Resets the index but not the working tree (i.e., the changed files
  36        are preserved but not marked for commit) and reports what has not
  37        been updated. This is the default action.
  38
  39--soft::
  40        Does not touch the index file nor the working tree at all, but
  41        requires them to be in a good order. This leaves all your changed
  42        files "Changes to be committed", as 'git status' would
  43        put it.
  44
  45--hard::
  46        Matches the working tree and index to that of the tree being
  47        switched to. Any changes to tracked files in the working tree
  48        since <commit> are lost.
  49
  50--merge::
  51        Resets the index to match the tree recorded by the named commit,
  52        and updates the files that are different between the named commit
  53        and the current commit in the working tree.
  54
  55--keep::
  56        Resets the index to match the tree recorded by the named commit,
  57        but keep changes in the working tree. Aborts if the reset would
  58        change files that are already modified in the working tree.
  59
  60-p::
  61--patch::
  62        Interactively select hunks in the difference between the index
  63        and <commit> (defaults to HEAD).  The chosen hunks are applied
  64        in reverse to the index.
  65+
  66This means that `git reset -p` is the opposite of `git add -p` (see
  67linkgit:git-add[1]).
  68
  69-q::
  70--quiet::
  71        Be quiet, only report errors.
  72
  73<commit>::
  74        Commit to make the current HEAD. If not given defaults to HEAD.
  75
  76DISCUSSION
  77----------
  78
  79The tables below show what happens when running:
  80
  81----------
  82git reset --option target
  83----------
  84
  85to reset the HEAD to another commit (`target`) with the different
  86reset options depending on the state of the files.
  87
  88In these tables, A, B, C and D are some different states of a
  89file. For example, the first line of the first table means that if a
  90file is in state A in the working tree, in state B in the index, in
  91state C in HEAD and in state D in the target, then "git reset --soft
  92target" will put the file in state A in the working tree, in state B
  93in the index and in state D in HEAD.
  94
  95      working index HEAD target         working index HEAD
  96      ----------------------------------------------------
  97       A       B     C    D     --soft   A       B     D
  98                                --mixed  A       D     D
  99                                --hard   D       D     D
 100                                --merge (disallowed)
 101                                --keep  (disallowed)
 102
 103      working index HEAD target         working index HEAD
 104      ----------------------------------------------------
 105       A       B     C    C     --soft   A       B     C
 106                                --mixed  A       C     C
 107                                --hard   C       C     C
 108                                --merge (disallowed)
 109                                --keep   A       C     C
 110
 111      working index HEAD target         working index HEAD
 112      ----------------------------------------------------
 113       B       B     C    D     --soft   B       B     D
 114                                --mixed  B       D     D
 115                                --hard   D       D     D
 116                                --merge  D       D     D
 117                                --keep  (disallowed)
 118
 119      working index HEAD target         working index HEAD
 120      ----------------------------------------------------
 121       B       B     C    C     --soft   B       B     C
 122                                --mixed  B       C     C
 123                                --hard   C       C     C
 124                                --merge  C       C     C
 125                                --keep   B       C     C
 126
 127      working index HEAD target         working index HEAD
 128      ----------------------------------------------------
 129       B       C     C    D     --soft   B       C     D
 130                                --mixed  B       D     D
 131                                --hard   D       D     D
 132                                --merge (disallowed)
 133                                --keep  (disallowed)
 134
 135      working index HEAD target         working index HEAD
 136      ----------------------------------------------------
 137       B       C     C    C     --soft   B       C     C
 138                                --mixed  B       C     C
 139                                --hard   C       C     C
 140                                --merge  B       C     C
 141                                --keep   B       C     C
 142
 143"reset --merge" is meant to be used when resetting out of a conflicted
 144merge. Any mergy operation guarantees that the work tree file that is
 145involved in the merge does not have local change wrt the index before
 146it starts, and that it writes the result out to the work tree. So if
 147we see some difference between the index and the target and also
 148between the index and the work tree, then it means that we are not
 149resetting out from a state that a mergy operation left after failing
 150with a conflict. That is why we disallow --merge option in this case.
 151
 152"reset --keep" is meant to be used when removing some of the last
 153commits in the current branch while keeping changes in the working
 154tree. If there could be conflicts between the changes in the commit we
 155want to remove and the changes in the working tree we want to keep,
 156the reset is disallowed. That's why it is disallowed if there are both
 157changes between the working tree and HEAD, and between HEAD and the
 158target. To be safe, it is also disallowed when there are unmerged
 159entries.
 160
 161The following tables show what happens when there are unmerged
 162entries:
 163
 164      working index HEAD target         working index HEAD
 165      ----------------------------------------------------
 166       X       U     A    B     --soft  (disallowed)
 167                                --mixed  X       B     B
 168                                --hard   B       B     B
 169                                --merge  B       B     B
 170                                --keep  (disallowed)
 171
 172      working index HEAD target         working index HEAD
 173      ----------------------------------------------------
 174       X       U     A    A     --soft  (disallowed)
 175                                --mixed  X       A     A
 176                                --hard   A       A     A
 177                                --merge  A       A     A
 178                                --keep  (disallowed)
 179
 180X means any state and U means an unmerged index.
 181
 182Examples
 183--------
 184
 185Undo a commit and redo::
 186+
 187------------
 188$ git commit ...
 189$ git reset --soft HEAD^      <1>
 190$ edit                        <2>
 191$ git commit -a -c ORIG_HEAD  <3>
 192------------
 193+
 194<1> This is most often done when you remembered what you
 195just committed is incomplete, or you misspelled your commit
 196message, or both.  Leaves working tree as it was before "reset".
 197<2> Make corrections to working tree files.
 198<3> "reset" copies the old head to .git/ORIG_HEAD; redo the
 199commit by starting with its log message.  If you do not need to
 200edit the message further, you can give -C option instead.
 201+
 202See also the --amend option to linkgit:git-commit[1].
 203
 204Undo commits permanently::
 205+
 206------------
 207$ git commit ...
 208$ git reset --hard HEAD~3   <1>
 209------------
 210+
 211<1> The last three commits (HEAD, HEAD^, and HEAD~2) were bad
 212and you do not want to ever see them again.  Do *not* do this if
 213you have already given these commits to somebody else.  (See the
 214"RECOVERING FROM UPSTREAM REBASE" section in linkgit:git-rebase[1] for
 215the implications of doing so.)
 216
 217Undo a commit, making it a topic branch::
 218+
 219------------
 220$ git branch topic/wip     <1>
 221$ git reset --hard HEAD~3  <2>
 222$ git checkout topic/wip   <3>
 223------------
 224+
 225<1> You have made some commits, but realize they were premature
 226to be in the "master" branch.  You want to continue polishing
 227them in a topic branch, so create "topic/wip" branch off of the
 228current HEAD.
 229<2> Rewind the master branch to get rid of those three commits.
 230<3> Switch to "topic/wip" branch and keep working.
 231
 232Undo add::
 233+
 234------------
 235$ edit                                     <1>
 236$ git add frotz.c filfre.c
 237$ mailx                                    <2>
 238$ git reset                                <3>
 239$ git pull git://info.example.com/ nitfol  <4>
 240------------
 241+
 242<1> You are happily working on something, and find the changes
 243in these files are in good order.  You do not want to see them
 244when you run "git diff", because you plan to work on other files
 245and changes with these files are distracting.
 246<2> Somebody asks you to pull, and the changes sounds worthy of merging.
 247<3> However, you already dirtied the index (i.e. your index does
 248not match the HEAD commit).  But you know the pull you are going
 249to make does not affect frotz.c nor filfre.c, so you revert the
 250index changes for these two files.  Your changes in working tree
 251remain there.
 252<4> Then you can pull and merge, leaving frotz.c and filfre.c
 253changes still in the working tree.
 254
 255Undo a merge or pull::
 256+
 257------------
 258$ git pull                         <1>
 259Auto-merging nitfol
 260CONFLICT (content): Merge conflict in nitfol
 261Automatic merge failed; fix conflicts and then commit the result.
 262$ git reset --hard                 <2>
 263$ git pull . topic/branch          <3>
 264Updating from 41223... to 13134...
 265Fast-forward
 266$ git reset --hard ORIG_HEAD       <4>
 267------------
 268+
 269<1> Try to update from the upstream resulted in a lot of
 270conflicts; you were not ready to spend a lot of time merging
 271right now, so you decide to do that later.
 272<2> "pull" has not made merge commit, so "git reset --hard"
 273which is a synonym for "git reset --hard HEAD" clears the mess
 274from the index file and the working tree.
 275<3> Merge a topic branch into the current branch, which resulted
 276in a fast-forward.
 277<4> But you decided that the topic branch is not ready for public
 278consumption yet.  "pull" or "merge" always leaves the original
 279tip of the current branch in ORIG_HEAD, so resetting hard to it
 280brings your index file and the working tree back to that state,
 281and resets the tip of the branch to that commit.
 282
 283Undo a merge or pull inside a dirty work tree::
 284+
 285------------
 286$ git pull                         <1>
 287Auto-merging nitfol
 288Merge made by recursive.
 289 nitfol                |   20 +++++----
 290 ...
 291$ git reset --merge ORIG_HEAD      <2>
 292------------
 293+
 294<1> Even if you may have local modifications in your
 295working tree, you can safely say "git pull" when you know
 296that the change in the other branch does not overlap with
 297them.
 298<2> After inspecting the result of the merge, you may find
 299that the change in the other branch is unsatisfactory.  Running
 300"git reset --hard ORIG_HEAD" will let you go back to where you
 301were, but it will discard your local changes, which you do not
 302want.  "git reset --merge" keeps your local changes.
 303
 304
 305Interrupted workflow::
 306+
 307Suppose you are interrupted by an urgent fix request while you
 308are in the middle of a large change.  The files in your
 309working tree are not in any shape to be committed yet, but you
 310need to get to the other branch for a quick bugfix.
 311+
 312------------
 313$ git checkout feature ;# you were working in "feature" branch and
 314$ work work work       ;# got interrupted
 315$ git commit -a -m "snapshot WIP"                 <1>
 316$ git checkout master
 317$ fix fix fix
 318$ git commit ;# commit with real log
 319$ git checkout feature
 320$ git reset --soft HEAD^ ;# go back to WIP state  <2>
 321$ git reset                                       <3>
 322------------
 323+
 324<1> This commit will get blown away so a throw-away log message is OK.
 325<2> This removes the 'WIP' commit from the commit history, and sets
 326    your working tree to the state just before you made that snapshot.
 327<3> At this point the index file still has all the WIP changes you
 328    committed as 'snapshot WIP'.  This updates the index to show your
 329    WIP files as uncommitted.
 330+
 331See also linkgit:git-stash[1].
 332
 333Reset a single file in the index::
 334+
 335Suppose you have added a file to your index, but later decide you do not
 336want to add it to your commit. You can remove the file from the index
 337while keeping your changes with git reset.
 338+
 339------------
 340$ git reset -- frotz.c                      <1>
 341$ git commit -m "Commit files in index"     <2>
 342$ git add frotz.c                           <3>
 343------------
 344+
 345<1> This removes the file from the index while keeping it in the working
 346    directory.
 347<2> This commits all other changes in the index.
 348<3> Adds the file to the index again.
 349
 350Keep changes in working tree while discarding some previous commits::
 351+
 352Suppose you are working on something and you commit it, and then you
 353continue working a bit more, but now you think that what you have in
 354your working tree should be in another branch that has nothing to do
 355with what you commited previously. You can start a new branch and
 356reset it while keeping the changes in your work tree.
 357+
 358------------
 359$ git tag start
 360$ git checkout -b branch1
 361$ edit
 362$ git commit ...                            <1>
 363$ edit
 364$ git checkout -b branch2                   <2>
 365$ git reset --keep start                    <3>
 366------------
 367+
 368<1> This commits your first edits in branch1.
 369<2> In the ideal world, you could have realized that the earlier
 370    commit did not belong to the new topic when you created and switched
 371    to branch2 (i.e. "git checkout -b branch2 start"), but nobody is
 372    perfect.
 373<3> But you can use "reset --keep" to remove the unwanted commit after
 374    you switched to "branch2".
 375
 376Author
 377------
 378Written by Junio C Hamano <gitster@pobox.com> and Linus Torvalds <torvalds@osdl.org>
 379
 380Documentation
 381--------------
 382Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
 383
 384GIT
 385---
 386Part of the linkgit:git[1] suite