1 git-reset(1)
2 ============
4 NAME
5 ----
6 git-reset - Reset current HEAD to the specified state
8 SYNOPSIS
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>]
15 DESCRIPTION
16 -----------
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
20 in all forms.
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 +
27 This means that `git reset <paths>` is the opposite of `git add
28 <paths>`.
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 +
35 This means that `git reset -p` is the opposite of `git add -p` (see
36 linkgit:git-add[1]).
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.
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.
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.
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.
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 --
74 If you want to undo a commit other than the latest on a branch,
75 linkgit:git-revert[1] is your friend.
78 OPTIONS
79 -------
81 -q::
82 --quiet::
83 Be quiet, only report errors.
86 EXAMPLES
87 --------
89 Undo a commit and redo::
90 +
91 ------------
92 $ git commit ...
93 $ git reset --soft HEAD^ <1>
94 $ edit <2>
95 $ git commit -a -c ORIG_HEAD <3>
96 ------------
97 +
98 <1> This is most often done when you remembered what you
99 just committed is incomplete, or you misspelled your commit
100 message, or both. Leaves working tree as it was before "reset".
101 <2> Make corrections to working tree files.
102 <3> "reset" copies the old head to .git/ORIG_HEAD; redo the
103 commit by starting with its log message. If you do not need to
104 edit the message further, you can give -C option instead.
105 +
106 See also the --amend option to linkgit:git-commit[1].
108 Undo commits permanently::
109 +
110 ------------
111 $ git commit ...
112 $ git reset --hard HEAD~3 <1>
113 ------------
114 +
115 <1> The last three commits (HEAD, HEAD^, and HEAD~2) were bad
116 and you do not want to ever see them again. Do *not* do this if
117 you have already given these commits to somebody else. (See the
118 "RECOVERING FROM UPSTREAM REBASE" section in linkgit:git-rebase[1] for
119 the implications of doing so.)
121 Undo a commit, making it a topic branch::
122 +
123 ------------
124 $ git branch topic/wip <1>
125 $ git reset --hard HEAD~3 <2>
126 $ git checkout topic/wip <3>
127 ------------
128 +
129 <1> You have made some commits, but realize they were premature
130 to be in the "master" branch. You want to continue polishing
131 them in a topic branch, so create "topic/wip" branch off of the
132 current HEAD.
133 <2> Rewind the master branch to get rid of those three commits.
134 <3> Switch to "topic/wip" branch and keep working.
136 Undo add::
137 +
138 ------------
139 $ edit <1>
140 $ git add frotz.c filfre.c
141 $ mailx <2>
142 $ git reset <3>
143 $ git pull git://info.example.com/ nitfol <4>
144 ------------
145 +
146 <1> You are happily working on something, and find the changes
147 in these files are in good order. You do not want to see them
148 when you run "git diff", because you plan to work on other files
149 and changes with these files are distracting.
150 <2> Somebody asks you to pull, and the changes sounds worthy of merging.
151 <3> However, you already dirtied the index (i.e. your index does
152 not match the HEAD commit). But you know the pull you are going
153 to make does not affect frotz.c nor filfre.c, so you revert the
154 index changes for these two files. Your changes in working tree
155 remain there.
156 <4> Then you can pull and merge, leaving frotz.c and filfre.c
157 changes still in the working tree.
159 Undo a merge or pull::
160 +
161 ------------
162 $ git pull <1>
163 Auto-merging nitfol
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...
169 Fast-forward
170 $ git reset --hard ORIG_HEAD <4>
171 ------------
172 +
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
180 in a fast-forward.
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::
188 +
189 ------------
190 $ git pull <1>
191 Auto-merging nitfol
192 Merge made by recursive.
193 nitfol | 20 +++++----
194 ...
195 $ git reset --merge ORIG_HEAD <2>
196 ------------
197 +
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
201 them.
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::
210 +
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.
215 +
216 ------------
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
221 $ fix fix fix
222 $ git commit ;# commit with real log
223 $ git checkout feature
224 $ git reset --soft HEAD^ ;# go back to WIP state <2>
225 $ git reset <3>
226 ------------
227 +
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.
234 +
235 See also linkgit:git-stash[1].
237 Reset a single file in the index::
238 +
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.
242 +
243 ------------
244 $ git reset -- frotz.c <1>
245 $ git commit -m "Commit files in index" <2>
246 $ git add frotz.c <3>
247 ------------
248 +
249 <1> This removes the file from the index while keeping it in the working
250 directory.
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::
255 +
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 commited previously. You can start a new branch and
260 reset it while keeping the changes in your work tree.
261 +
262 ------------
263 $ git tag start
264 $ git checkout -b branch1
265 $ edit
266 $ git commit ... <1>
267 $ edit
268 $ git checkout -b branch2 <2>
269 $ git reset --keep start <3>
270 ------------
271 +
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
276 perfect.
277 <3> But you can use "reset --keep" to remove the unwanted commit after
278 you switched to "branch2".
281 DISCUSSION
282 ----------
284 The tables below show what happens when running:
286 ----------
287 git reset --option target
288 ----------
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 ----------------------------------------------------
302 A B C D --soft A B D
303 --mixed A D D
304 --hard D D D
305 --merge (disallowed)
306 --keep (disallowed)
308 working index HEAD target working index HEAD
309 ----------------------------------------------------
310 A B C C --soft A B C
311 --mixed A C C
312 --hard C C C
313 --merge (disallowed)
314 --keep A C C
316 working index HEAD target working index HEAD
317 ----------------------------------------------------
318 B B C D --soft B B D
319 --mixed B D D
320 --hard D D D
321 --merge D D D
322 --keep (disallowed)
324 working index HEAD target working index HEAD
325 ----------------------------------------------------
326 B B C C --soft B B C
327 --mixed B C C
328 --hard C C C
329 --merge C C C
330 --keep B C C
332 working index HEAD target working index HEAD
333 ----------------------------------------------------
334 B C C D --soft B C D
335 --mixed B D D
336 --hard D D D
337 --merge (disallowed)
338 --keep (disallowed)
340 working index HEAD target working index HEAD
341 ----------------------------------------------------
342 B C C C --soft B C C
343 --mixed B C C
344 --hard C C C
345 --merge B C C
346 --keep B C C
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
364 entries.
366 The following tables show what happens when there are unmerged
367 entries:
369 working index HEAD target working index HEAD
370 ----------------------------------------------------
371 X U A B --soft (disallowed)
372 --mixed X B B
373 --hard B B B
374 --merge B B B
375 --keep (disallowed)
377 working index HEAD target working index HEAD
378 ----------------------------------------------------
379 X U A A --soft (disallowed)
380 --mixed X A A
381 --hard A A A
382 --merge A A A
383 --keep (disallowed)
385 X means any state and U means an unmerged index.
388 Author
389 ------
390 Written by Junio C Hamano <gitster@pobox.com> and Linus Torvalds <torvalds@osdl.org>
392 Documentation
393 --------------
394 Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
396 GIT
397 ---
398 Part of the linkgit:git[1] suite