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