Documentation/reset: separate options by mode
[git/jnareb-git.git] / Documentation / git-reset.txt
blobdca75b302d999eca6c3b26df324601b097524f8a
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.)
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.
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:
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.
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 DISCUSSION
87 ----------
89 The tables below show what happens when running:
91 ----------
92 git reset --option target
93 ----------
95 to reset the HEAD to another commit (`target`) with the different
96 reset options depending on the state of the files.
98 In these tables, A, B, C and D are some different states of a
99 file. For example, the first line of the first table means that if a
100 file is in state A in the working tree, in state B in the index, in
101 state C in HEAD and in state D in the target, then "git reset --soft
102 target" will put the file in state A in the working tree, in state B
103 in the index and in state D in HEAD.
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)
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
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)
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
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)
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
153 "reset --merge" is meant to be used when resetting out of a conflicted
154 merge. Any mergy operation guarantees that the work tree file that is
155 involved in the merge does not have local change wrt the index before
156 it starts, and that it writes the result out to the work tree. So if
157 we see some difference between the index and the target and also
158 between the index and the work tree, then it means that we are not
159 resetting out from a state that a mergy operation left after failing
160 with a conflict. That is why we disallow --merge option in this case.
162 "reset --keep" is meant to be used when removing some of the last
163 commits in the current branch while keeping changes in the working
164 tree. If there could be conflicts between the changes in the commit we
165 want to remove and the changes in the working tree we want to keep,
166 the reset is disallowed. That's why it is disallowed if there are both
167 changes between the working tree and HEAD, and between HEAD and the
168 target. To be safe, it is also disallowed when there are unmerged
169 entries.
171 The following tables show what happens when there are unmerged
172 entries:
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)
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)
190 X means any state and U means an unmerged index.
192 Examples
193 --------
195 Undo a commit and redo::
197 ------------
198 $ git commit ...
199 $ git reset --soft HEAD^      <1>
200 $ edit                        <2>
201 $ git commit -a -c ORIG_HEAD  <3>
202 ------------
204 <1> This is most often done when you remembered what you
205 just committed is incomplete, or you misspelled your commit
206 message, 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
209 commit by starting with its log message.  If you do not need to
210 edit the message further, you can give -C option instead.
212 See also the --amend option to linkgit:git-commit[1].
214 Undo commits permanently::
216 ------------
217 $ git commit ...
218 $ git reset --hard HEAD~3   <1>
219 ------------
221 <1> The last three commits (HEAD, HEAD^, and HEAD~2) were bad
222 and you do not want to ever see them again.  Do *not* do this if
223 you have already given these commits to somebody else.  (See the
224 "RECOVERING FROM UPSTREAM REBASE" section in linkgit:git-rebase[1] for
225 the implications of doing so.)
227 Undo a commit, making it a topic branch::
229 ------------
230 $ git branch topic/wip     <1>
231 $ git reset --hard HEAD~3  <2>
232 $ git checkout topic/wip   <3>
233 ------------
235 <1> You have made some commits, but realize they were premature
236 to be in the "master" branch.  You want to continue polishing
237 them in a topic branch, so create "topic/wip" branch off of the
238 current HEAD.
239 <2> Rewind the master branch to get rid of those three commits.
240 <3> Switch to "topic/wip" branch and keep working.
242 Undo add::
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 ------------
252 <1> You are happily working on something, and find the changes
253 in these files are in good order.  You do not want to see them
254 when you run "git diff", because you plan to work on other files
255 and 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
258 not match the HEAD commit).  But you know the pull you are going
259 to make does not affect frotz.c nor filfre.c, so you revert the
260 index changes for these two files.  Your changes in working tree
261 remain there.
262 <4> Then you can pull and merge, leaving frotz.c and filfre.c
263 changes still in the working tree.
265 Undo a merge or pull::
267 ------------
268 $ git pull                         <1>
269 Auto-merging nitfol
270 CONFLICT (content): Merge conflict in nitfol
271 Automatic merge failed; fix conflicts and then commit the result.
272 $ git reset --hard                 <2>
273 $ git pull . topic/branch          <3>
274 Updating from 41223... to 13134...
275 Fast-forward
276 $ git reset --hard ORIG_HEAD       <4>
277 ------------
279 <1> Try to update from the upstream resulted in a lot of
280 conflicts; you were not ready to spend a lot of time merging
281 right now, so you decide to do that later.
282 <2> "pull" has not made merge commit, so "git reset --hard"
283 which is a synonym for "git reset --hard HEAD" clears the mess
284 from the index file and the working tree.
285 <3> Merge a topic branch into the current branch, which resulted
286 in a fast-forward.
287 <4> But you decided that the topic branch is not ready for public
288 consumption yet.  "pull" or "merge" always leaves the original
289 tip of the current branch in ORIG_HEAD, so resetting hard to it
290 brings your index file and the working tree back to that state,
291 and resets the tip of the branch to that commit.
293 Undo a merge or pull inside a dirty work tree::
295 ------------
296 $ git pull                         <1>
297 Auto-merging nitfol
298 Merge made by recursive.
299  nitfol                |   20 +++++----
300  ...
301 $ git reset --merge ORIG_HEAD      <2>
302 ------------
304 <1> Even if you may have local modifications in your
305 working tree, you can safely say "git pull" when you know
306 that the change in the other branch does not overlap with
307 them.
308 <2> After inspecting the result of the merge, you may find
309 that the change in the other branch is unsatisfactory.  Running
310 "git reset --hard ORIG_HEAD" will let you go back to where you
311 were, but it will discard your local changes, which you do not
312 want.  "git reset --merge" keeps your local changes.
315 Interrupted workflow::
317 Suppose you are interrupted by an urgent fix request while you
318 are in the middle of a large change.  The files in your
319 working tree are not in any shape to be committed yet, but you
320 need to get to the other branch for a quick bugfix.
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 ------------
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.
341 See also linkgit:git-stash[1].
343 Reset a single file in the index::
345 Suppose you have added a file to your index, but later decide you do not
346 want to add it to your commit. You can remove the file from the index
347 while keeping your changes with git reset.
349 ------------
350 $ git reset -- frotz.c                      <1>
351 $ git commit -m "Commit files in index"     <2>
352 $ git add frotz.c                           <3>
353 ------------
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.
360 Keep changes in working tree while discarding some previous commits::
362 Suppose you are working on something and you commit it, and then you
363 continue working a bit more, but now you think that what you have in
364 your working tree should be in another branch that has nothing to do
365 with what you commited previously. You can start a new branch and
366 reset it while keeping the changes in your work tree.
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 ------------
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".
386 Author
387 ------
388 Written by Junio C Hamano <gitster@pobox.com> and Linus Torvalds <torvalds@osdl.org>
390 Documentation
391 --------------
392 Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
396 Part of the linkgit:git[1] suite