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