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