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