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