Documentation / git-reset.txton commit Merge branch 'jk/maint-1.6.5-reset-hard' (0b4ae29)
   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
  71Examples
  72--------
  73
  74Undo a commit and redo::
  75+
  76------------
  77$ git commit ...
  78$ git reset --soft HEAD^      <1>
  79$ edit                        <2>
  80$ git commit -a -c ORIG_HEAD  <3>
  81------------
  82+
  83<1> This is most often done when you remembered what you
  84just committed is incomplete, or you misspelled your commit
  85message, or both.  Leaves working tree as it was before "reset".
  86<2> Make corrections to working tree files.
  87<3> "reset" copies the old head to .git/ORIG_HEAD; redo the
  88commit by starting with its log message.  If you do not need to
  89edit the message further, you can give -C option instead.
  90+
  91See also the --amend option to linkgit:git-commit[1].
  92
  93Undo commits permanently::
  94+
  95------------
  96$ git commit ...
  97$ git reset --hard HEAD~3   <1>
  98------------
  99+
 100<1> The last three commits (HEAD, HEAD^, and HEAD~2) were bad
 101and you do not want to ever see them again.  Do *not* do this if
 102you have already given these commits to somebody else.  (See the
 103"RECOVERING FROM UPSTREAM REBASE" section in linkgit:git-rebase[1] for
 104the implications of doing so.)
 105
 106Undo a commit, making it a topic branch::
 107+
 108------------
 109$ git branch topic/wip     <1>
 110$ git reset --hard HEAD~3  <2>
 111$ git checkout topic/wip   <3>
 112------------
 113+
 114<1> You have made some commits, but realize they were premature
 115to be in the "master" branch.  You want to continue polishing
 116them in a topic branch, so create "topic/wip" branch off of the
 117current HEAD.
 118<2> Rewind the master branch to get rid of those three commits.
 119<3> Switch to "topic/wip" branch and keep working.
 120
 121Undo add::
 122+
 123------------
 124$ edit                                     <1>
 125$ git add frotz.c filfre.c
 126$ mailx                                    <2>
 127$ git reset                                <3>
 128$ git pull git://info.example.com/ nitfol  <4>
 129------------
 130+
 131<1> You are happily working on something, and find the changes
 132in these files are in good order.  You do not want to see them
 133when you run "git diff", because you plan to work on other files
 134and changes with these files are distracting.
 135<2> Somebody asks you to pull, and the changes sounds worthy of merging.
 136<3> However, you already dirtied the index (i.e. your index does
 137not match the HEAD commit).  But you know the pull you are going
 138to make does not affect frotz.c nor filfre.c, so you revert the
 139index changes for these two files.  Your changes in working tree
 140remain there.
 141<4> Then you can pull and merge, leaving frotz.c and filfre.c
 142changes still in the working tree.
 143
 144Undo a merge or pull::
 145+
 146------------
 147$ git pull                         <1>
 148Auto-merging nitfol
 149CONFLICT (content): Merge conflict in nitfol
 150Automatic merge failed; fix conflicts and then commit the result.
 151$ git reset --hard                 <2>
 152$ git pull . topic/branch          <3>
 153Updating from 41223... to 13134...
 154Fast-forward
 155$ git reset --hard ORIG_HEAD       <4>
 156------------
 157+
 158<1> Try to update from the upstream resulted in a lot of
 159conflicts; you were not ready to spend a lot of time merging
 160right now, so you decide to do that later.
 161<2> "pull" has not made merge commit, so "git reset --hard"
 162which is a synonym for "git reset --hard HEAD" clears the mess
 163from the index file and the working tree.
 164<3> Merge a topic branch into the current branch, which resulted
 165in a fast-forward.
 166<4> But you decided that the topic branch is not ready for public
 167consumption yet.  "pull" or "merge" always leaves the original
 168tip of the current branch in ORIG_HEAD, so resetting hard to it
 169brings your index file and the working tree back to that state,
 170and resets the tip of the branch to that commit.
 171
 172Undo a merge or pull inside a dirty work tree::
 173+
 174------------
 175$ git pull                         <1>
 176Auto-merging nitfol
 177Merge made by recursive.
 178 nitfol                |   20 +++++----
 179 ...
 180$ git reset --merge ORIG_HEAD      <2>
 181------------
 182+
 183<1> Even if you may have local modifications in your
 184working tree, you can safely say "git pull" when you know
 185that the change in the other branch does not overlap with
 186them.
 187<2> After inspecting the result of the merge, you may find
 188that the change in the other branch is unsatisfactory.  Running
 189"git reset --hard ORIG_HEAD" will let you go back to where you
 190were, but it will discard your local changes, which you do not
 191want.  "git reset --merge" keeps your local changes.
 192
 193
 194Interrupted workflow::
 195+
 196Suppose you are interrupted by an urgent fix request while you
 197are in the middle of a large change.  The files in your
 198working tree are not in any shape to be committed yet, but you
 199need to get to the other branch for a quick bugfix.
 200+
 201------------
 202$ git checkout feature ;# you were working in "feature" branch and
 203$ work work work       ;# got interrupted
 204$ git commit -a -m "snapshot WIP"                 <1>
 205$ git checkout master
 206$ fix fix fix
 207$ git commit ;# commit with real log
 208$ git checkout feature
 209$ git reset --soft HEAD^ ;# go back to WIP state  <2>
 210$ git reset                                       <3>
 211------------
 212+
 213<1> This commit will get blown away so a throw-away log message is OK.
 214<2> This removes the 'WIP' commit from the commit history, and sets
 215    your working tree to the state just before you made that snapshot.
 216<3> At this point the index file still has all the WIP changes you
 217    committed as 'snapshot WIP'.  This updates the index to show your
 218    WIP files as uncommitted.
 219+
 220See also linkgit:git-stash[1].
 221
 222Reset a single file in the index::
 223+
 224Suppose you have added a file to your index, but later decide you do not
 225want to add it to your commit. You can remove the file from the index
 226while keeping your changes with git reset.
 227+
 228------------
 229$ git reset -- frotz.c                      <1>
 230$ git commit -m "Commit files in index"     <2>
 231$ git add frotz.c                           <3>
 232------------
 233+
 234<1> This removes the file from the index while keeping it in the working
 235    directory.
 236<2> This commits all other changes in the index.
 237<3> Adds the file to the index again.
 238
 239Author
 240------
 241Written by Junio C Hamano <gitster@pobox.com> and Linus Torvalds <torvalds@osdl.org>
 242
 243Documentation
 244--------------
 245Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
 246
 247GIT
 248---
 249Part of the linkgit:git[1] suite