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