6 git-reset - Reset current HEAD to the specified state
11 'git reset' [-q] [<commit>] [--] <paths>...
12 'git reset' --patch [<commit>] [--] [<paths>...]
13 'git reset' [--soft | --mixed | --hard | --merge | --keep] [-q] [<commit>]
17 In the first and second form, copy entries from <commit> to the index.
18 In the third form, set the current branch to <commit>, optionally
19 modifying index and worktree to match. The <commit> defaults to HEAD
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
27 This means that `git reset <paths>` is the opposite of `git add
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.
35 This means that `git reset -p` is the opposite of `git add -p` (see
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:
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
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.
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.
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.
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
74 If you want to undo a commit other than the latest on a branch,
75 linkgit:git-revert[1] is your friend.
83 Be quiet, only report errors.
93 $ git add frotz.c filfre.c
96 $ git pull git://info.example.com/ nitfol <4>
99 <1> You are happily working on something, and find the changes
100 in these files are in good order. You do not want to see them
101 when you run "git diff", because you plan to work on other files
102 and changes with these files are distracting.
103 <2> Somebody asks you to pull, and the changes sounds worthy of merging.
104 <3> However, you already dirtied the index (i.e. your index does
105 not match the HEAD commit). But you know the pull you are going
106 to make does not affect frotz.c nor filfre.c, so you revert the
107 index changes for these two files. Your changes in working tree
109 <4> Then you can pull and merge, leaving frotz.c and filfre.c
110 changes still in the working tree.
112 Undo a commit and redo::
116 $ git reset --soft HEAD^ <1>
118 $ git commit -a -c ORIG_HEAD <3>
121 <1> This is most often done when you remembered what you
122 just committed is incomplete, or you misspelled your commit
123 message, or both. Leaves working tree as it was before "reset".
124 <2> Make corrections to working tree files.
125 <3> "reset" copies the old head to .git/ORIG_HEAD; redo the
126 commit by starting with its log message. If you do not need to
127 edit the message further, you can give -C option instead.
129 See also the --amend option to linkgit:git-commit[1].
131 Undo a commit, making it a topic branch::
134 $ git branch topic/wip <1>
135 $ git reset --hard HEAD~3 <2>
136 $ git checkout topic/wip <3>
139 <1> You have made some commits, but realize they were premature
140 to be in the "master" branch. You want to continue polishing
141 them in a topic branch, so create "topic/wip" branch off of the
143 <2> Rewind the master branch to get rid of those three commits.
144 <3> Switch to "topic/wip" branch and keep working.
146 Undo commits permanently::
150 $ git reset --hard HEAD~3 <1>
153 <1> The last three commits (HEAD, HEAD^, and HEAD~2) were bad
154 and you do not want to ever see them again. Do *not* do this if
155 you have already given these commits to somebody else. (See the
156 "RECOVERING FROM UPSTREAM REBASE" section in linkgit:git-rebase[1] for
157 the implications of doing so.)
159 Undo a merge or pull::
164 CONFLICT (content): Merge conflict in nitfol
165 Automatic merge failed; fix conflicts and then commit the result.
166 $ git reset --hard <2>
167 $ git pull . topic/branch <3>
168 Updating from 41223... to 13134...
170 $ git reset --hard ORIG_HEAD <4>
173 <1> Try to update from the upstream resulted in a lot of
174 conflicts; you were not ready to spend a lot of time merging
175 right now, so you decide to do that later.
176 <2> "pull" has not made merge commit, so "git reset --hard"
177 which is a synonym for "git reset --hard HEAD" clears the mess
178 from the index file and the working tree.
179 <3> Merge a topic branch into the current branch, which resulted
181 <4> But you decided that the topic branch is not ready for public
182 consumption yet. "pull" or "merge" always leaves the original
183 tip of the current branch in ORIG_HEAD, so resetting hard to it
184 brings your index file and the working tree back to that state,
185 and resets the tip of the branch to that commit.
187 Undo a merge or pull inside a dirty work tree::
192 Merge made by recursive.
193 nitfol | 20 +++++----
195 $ git reset --merge ORIG_HEAD <2>
198 <1> Even if you may have local modifications in your
199 working tree, you can safely say "git pull" when you know
200 that the change in the other branch does not overlap with
202 <2> After inspecting the result of the merge, you may find
203 that the change in the other branch is unsatisfactory. Running
204 "git reset --hard ORIG_HEAD" will let you go back to where you
205 were, but it will discard your local changes, which you do not
206 want. "git reset --merge" keeps your local changes.
209 Interrupted workflow::
211 Suppose you are interrupted by an urgent fix request while you
212 are in the middle of a large change. The files in your
213 working tree are not in any shape to be committed yet, but you
214 need to get to the other branch for a quick bugfix.
217 $ git checkout feature ;# you were working in "feature" branch and
218 $ work work work ;# got interrupted
219 $ git commit -a -m "snapshot WIP" <1>
220 $ git checkout master
222 $ git commit ;# commit with real log
223 $ git checkout feature
224 $ git reset --soft HEAD^ ;# go back to WIP state <2>
228 <1> This commit will get blown away so a throw-away log message is OK.
229 <2> This removes the 'WIP' commit from the commit history, and sets
230 your working tree to the state just before you made that snapshot.
231 <3> At this point the index file still has all the WIP changes you
232 committed as 'snapshot WIP'. This updates the index to show your
233 WIP files as uncommitted.
235 See also linkgit:git-stash[1].
237 Reset a single file in the index::
239 Suppose you have added a file to your index, but later decide you do not
240 want to add it to your commit. You can remove the file from the index
241 while keeping your changes with git reset.
244 $ git reset -- frotz.c <1>
245 $ git commit -m "Commit files in index" <2>
246 $ git add frotz.c <3>
249 <1> This removes the file from the index while keeping it in the working
251 <2> This commits all other changes in the index.
252 <3> Adds the file to the index again.
254 Keep changes in working tree while discarding some previous commits::
256 Suppose you are working on something and you commit it, and then you
257 continue working a bit more, but now you think that what you have in
258 your working tree should be in another branch that has nothing to do
259 with what you committed previously. You can start a new branch and
260 reset it while keeping the changes in your work tree.
264 $ git checkout -b branch1
268 $ git checkout -b branch2 <2>
269 $ git reset --keep start <3>
272 <1> This commits your first edits in branch1.
273 <2> In the ideal world, you could have realized that the earlier
274 commit did not belong to the new topic when you created and switched
275 to branch2 (i.e. "git checkout -b branch2 start"), but nobody is
277 <3> But you can use "reset --keep" to remove the unwanted commit after
278 you switched to "branch2".
284 The tables below show what happens when running:
287 git reset --option target
290 to reset the HEAD to another commit (`target`) with the different
291 reset options depending on the state of the files.
293 In these tables, A, B, C and D are some different states of a
294 file. For example, the first line of the first table means that if a
295 file is in state A in the working tree, in state B in the index, in
296 state C in HEAD and in state D in the target, then "git reset --soft
297 target" will put the file in state A in the working tree, in state B
298 in the index and in state D in HEAD.
300 working index HEAD target working index HEAD
301 ----------------------------------------------------
308 working index HEAD target working index HEAD
309 ----------------------------------------------------
316 working index HEAD target working index HEAD
317 ----------------------------------------------------
324 working index HEAD target working index HEAD
325 ----------------------------------------------------
332 working index HEAD target working index HEAD
333 ----------------------------------------------------
340 working index HEAD target working index HEAD
341 ----------------------------------------------------
348 "reset --merge" is meant to be used when resetting out of a conflicted
349 merge. Any mergy operation guarantees that the work tree file that is
350 involved in the merge does not have local change wrt the index before
351 it starts, and that it writes the result out to the work tree. So if
352 we see some difference between the index and the target and also
353 between the index and the work tree, then it means that we are not
354 resetting out from a state that a mergy operation left after failing
355 with a conflict. That is why we disallow --merge option in this case.
357 "reset --keep" is meant to be used when removing some of the last
358 commits in the current branch while keeping changes in the working
359 tree. If there could be conflicts between the changes in the commit we
360 want to remove and the changes in the working tree we want to keep,
361 the reset is disallowed. That's why it is disallowed if there are both
362 changes between the working tree and HEAD, and between HEAD and the
363 target. To be safe, it is also disallowed when there are unmerged
366 The following tables show what happens when there are unmerged
369 working index HEAD target working index HEAD
370 ----------------------------------------------------
371 X U A B --soft (disallowed)
377 working index HEAD target working index HEAD
378 ----------------------------------------------------
379 X U A A --soft (disallowed)
385 X means any state and U means an unmerged index.
390 Written by Junio C Hamano <gitster@pobox.com> and Linus Torvalds <torvalds@osdl.org>
394 Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
398 Part of the linkgit:git[1] suite