]> git.ipfire.org Git - thirdparty/git.git/blob - Documentation/git-reset.txt
branch.c: simplify advice-and-die sequence
[thirdparty/git.git] / Documentation / git-reset.txt
1 git-reset(1)
2 ============
3
4 NAME
5 ----
6 git-reset - Reset current HEAD to the specified state
7
8 SYNOPSIS
9 --------
10 [verse]
11 'git reset' [-q] [<tree-ish>] [--] <pathspec>...
12 'git reset' [-q] [--pathspec-from-file=<file> [--pathspec-file-nul]] [<tree-ish>]
13 'git reset' (--patch | -p) [<tree-ish>] [--] [<pathspec>...]
14 'git reset' [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]
15
16 DESCRIPTION
17 -----------
18 In the first three forms, copy entries from `<tree-ish>` to the index.
19 In the last form, set the current branch head (`HEAD`) to `<commit>`,
20 optionally modifying index and working tree to match.
21 The `<tree-ish>`/`<commit>` defaults to `HEAD` in all forms.
22
23 'git reset' [-q] [<tree-ish>] [--] <pathspec>...::
24 'git reset' [-q] [--pathspec-from-file=<file> [--pathspec-file-nul]] [<tree-ish>]::
25 These forms reset the index entries for all paths that match the
26 `<pathspec>` to their state at `<tree-ish>`. (It does not affect
27 the working tree or the current branch.)
28 +
29 This means that `git reset <pathspec>` is the opposite of `git add
30 <pathspec>`. This command is equivalent to
31 `git restore [--source=<tree-ish>] --staged <pathspec>...`.
32 +
33 After running `git reset <pathspec>` to update the index entry, you can
34 use linkgit:git-restore[1] to check the contents out of the index to
35 the working tree. Alternatively, using linkgit:git-restore[1]
36 and specifying a commit with `--source`, you
37 can copy the contents of a path out of a commit to the index and to the
38 working tree in one go.
39
40 'git reset' (--patch | -p) [<tree-ish>] [--] [<pathspec>...]::
41 Interactively select hunks in the difference between the index
42 and `<tree-ish>` (defaults to `HEAD`). The chosen hunks are applied
43 in reverse to the index.
44 +
45 This means that `git reset -p` is the opposite of `git add -p`, i.e.
46 you can use it to selectively reset hunks. See the ``Interactive Mode''
47 section of linkgit:git-add[1] to learn how to operate the `--patch` mode.
48
49 'git reset' [<mode>] [<commit>]::
50 This form resets the current branch head to `<commit>` and
51 possibly updates the index (resetting it to the tree of `<commit>`) and
52 the working tree depending on `<mode>`. If `<mode>` is omitted,
53 defaults to `--mixed`. The `<mode>` must be one of the following:
54 +
55 --
56 --soft::
57 Does not touch the index file or the working tree at all (but
58 resets the head to `<commit>`, just like all modes do). This leaves
59 all your changed files "Changes to be committed", as `git status`
60 would put it.
61
62 --mixed::
63 Resets the index but not the working tree (i.e., the changed files
64 are preserved but not marked for commit) and reports what has not
65 been updated. This is the default action.
66 +
67 If `-N` is specified, removed paths are marked as intent-to-add (see
68 linkgit:git-add[1]).
69
70 --hard::
71 Resets the index and working tree. Any changes to tracked files in the
72 working tree since `<commit>` are discarded. Any untracked files or
73 directories in the way of writing any tracked files are simply deleted.
74
75 --merge::
76 Resets the index and updates the files in the working tree that are
77 different between `<commit>` and `HEAD`, but keeps those which are
78 different between the index and working tree (i.e. which have changes
79 which have not been added).
80 If a file that is different between `<commit>` and the index has
81 unstaged changes, reset is aborted.
82 +
83 In other words, `--merge` does something like a `git read-tree -u -m <commit>`,
84 but carries forward unmerged index entries.
85
86 --keep::
87 Resets index entries and updates files in the working tree that are
88 different between `<commit>` and `HEAD`.
89 If a file that is different between `<commit>` and `HEAD` has local
90 changes, reset is aborted.
91
92 --[no-]recurse-submodules::
93 When the working tree is updated, using --recurse-submodules will
94 also recursively reset the working tree of all active submodules
95 according to the commit recorded in the superproject, also setting
96 the submodules' HEAD to be detached at that commit.
97 --
98
99 See "Reset, restore and revert" in linkgit:git[1] for the differences
100 between the three commands.
101
102
103 OPTIONS
104 -------
105
106 -q::
107 --quiet::
108 --no-quiet::
109 Be quiet, only report errors. The default behavior is set by the
110 `reset.quiet` config option. `--quiet` and `--no-quiet` will
111 override the default behavior.
112
113 --pathspec-from-file=<file>::
114 Pathspec is passed in `<file>` instead of commandline args. If
115 `<file>` is exactly `-` then standard input is used. Pathspec
116 elements are separated by LF or CR/LF. Pathspec elements can be
117 quoted as explained for the configuration variable `core.quotePath`
118 (see linkgit:git-config[1]). See also `--pathspec-file-nul` and
119 global `--literal-pathspecs`.
120
121 --pathspec-file-nul::
122 Only meaningful with `--pathspec-from-file`. Pathspec elements are
123 separated with NUL character and all other characters are taken
124 literally (including newlines and quotes).
125
126 \--::
127 Do not interpret any more arguments as options.
128
129 <pathspec>...::
130 Limits the paths affected by the operation.
131 +
132 For more details, see the 'pathspec' entry in linkgit:gitglossary[7].
133
134 EXAMPLES
135 --------
136
137 Undo add::
138 +
139 ------------
140 $ edit <1>
141 $ git add frotz.c filfre.c
142 $ mailx <2>
143 $ git reset <3>
144 $ git pull git://info.example.com/ nitfol <4>
145 ------------
146 +
147 <1> You are happily working on something, and find the changes
148 in these files are in good order. You do not want to see them
149 when you run `git diff`, because you plan to work on other files
150 and changes with these files are distracting.
151 <2> Somebody asks you to pull, and the changes sound worthy of merging.
152 <3> However, you already dirtied the index (i.e. your index does
153 not match the `HEAD` commit). But you know the pull you are going
154 to make does not affect `frotz.c` or `filfre.c`, so you revert the
155 index changes for these two files. Your changes in working tree
156 remain there.
157 <4> Then you can pull and merge, leaving `frotz.c` and `filfre.c`
158 changes still in the working tree.
159
160 Undo a commit and redo::
161 +
162 ------------
163 $ git commit ...
164 $ git reset --soft HEAD^ <1>
165 $ edit <2>
166 $ git commit -a -c ORIG_HEAD <3>
167 ------------
168 +
169 <1> This is most often done when you remembered what you
170 just committed is incomplete, or you misspelled your commit
171 message, or both. Leaves working tree as it was before "reset".
172 <2> Make corrections to working tree files.
173 <3> "reset" copies the old head to `.git/ORIG_HEAD`; redo the
174 commit by starting with its log message. If you do not need to
175 edit the message further, you can give `-C` option instead.
176 +
177 See also the `--amend` option to linkgit:git-commit[1].
178
179 Undo a commit, making it a topic branch::
180 +
181 ------------
182 $ git branch topic/wip <1>
183 $ git reset --hard HEAD~3 <2>
184 $ git switch topic/wip <3>
185 ------------
186 +
187 <1> You have made some commits, but realize they were premature
188 to be in the `master` branch. You want to continue polishing
189 them in a topic branch, so create `topic/wip` branch off of the
190 current `HEAD`.
191 <2> Rewind the master branch to get rid of those three commits.
192 <3> Switch to `topic/wip` branch and keep working.
193
194 Undo commits permanently::
195 +
196 ------------
197 $ git commit ...
198 $ git reset --hard HEAD~3 <1>
199 ------------
200 +
201 <1> The last three commits (`HEAD`, `HEAD^`, and `HEAD~2`) were bad
202 and you do not want to ever see them again. Do *not* do this if
203 you have already given these commits to somebody else. (See the
204 "RECOVERING FROM UPSTREAM REBASE" section in linkgit:git-rebase[1]
205 for the implications of doing so.)
206
207 Undo a merge or pull::
208 +
209 ------------
210 $ git pull <1>
211 Auto-merging nitfol
212 CONFLICT (content): Merge conflict in nitfol
213 Automatic merge failed; fix conflicts and then commit the result.
214 $ git reset --hard <2>
215 $ git pull . topic/branch <3>
216 Updating from 41223... to 13134...
217 Fast-forward
218 $ git reset --hard ORIG_HEAD <4>
219 ------------
220 +
221 <1> Try to update from the upstream resulted in a lot of
222 conflicts; you were not ready to spend a lot of time merging
223 right now, so you decide to do that later.
224 <2> "pull" has not made merge commit, so `git reset --hard`
225 which is a synonym for `git reset --hard HEAD` clears the mess
226 from the index file and the working tree.
227 <3> Merge a topic branch into the current branch, which resulted
228 in a fast-forward.
229 <4> But you decided that the topic branch is not ready for public
230 consumption yet. "pull" or "merge" always leaves the original
231 tip of the current branch in `ORIG_HEAD`, so resetting hard to it
232 brings your index file and the working tree back to that state,
233 and resets the tip of the branch to that commit.
234
235 Undo a merge or pull inside a dirty working tree::
236 +
237 ------------
238 $ git pull <1>
239 Auto-merging nitfol
240 Merge made by recursive.
241 nitfol | 20 +++++----
242 ...
243 $ git reset --merge ORIG_HEAD <2>
244 ------------
245 +
246 <1> Even if you may have local modifications in your
247 working tree, you can safely say `git pull` when you know
248 that the change in the other branch does not overlap with
249 them.
250 <2> After inspecting the result of the merge, you may find
251 that the change in the other branch is unsatisfactory. Running
252 `git reset --hard ORIG_HEAD` will let you go back to where you
253 were, but it will discard your local changes, which you do not
254 want. `git reset --merge` keeps your local changes.
255
256
257 Interrupted workflow::
258 +
259 Suppose you are interrupted by an urgent fix request while you
260 are in the middle of a large change. The files in your
261 working tree are not in any shape to be committed yet, but you
262 need to get to the other branch for a quick bugfix.
263 +
264 ------------
265 $ git switch feature ;# you were working in "feature" branch and
266 $ work work work ;# got interrupted
267 $ git commit -a -m "snapshot WIP" <1>
268 $ git switch master
269 $ fix fix fix
270 $ git commit ;# commit with real log
271 $ git switch feature
272 $ git reset --soft HEAD^ ;# go back to WIP state <2>
273 $ git reset <3>
274 ------------
275 +
276 <1> This commit will get blown away so a throw-away log message is OK.
277 <2> This removes the 'WIP' commit from the commit history, and sets
278 your working tree to the state just before you made that snapshot.
279 <3> At this point the index file still has all the WIP changes you
280 committed as 'snapshot WIP'. This updates the index to show your
281 WIP files as uncommitted.
282 +
283 See also linkgit:git-stash[1].
284
285 Reset a single file in the index::
286 +
287 Suppose you have added a file to your index, but later decide you do not
288 want to add it to your commit. You can remove the file from the index
289 while keeping your changes with git reset.
290 +
291 ------------
292 $ git reset -- frotz.c <1>
293 $ git commit -m "Commit files in index" <2>
294 $ git add frotz.c <3>
295 ------------
296 +
297 <1> This removes the file from the index while keeping it in the working
298 directory.
299 <2> This commits all other changes in the index.
300 <3> Adds the file to the index again.
301
302 Keep changes in working tree while discarding some previous commits::
303 +
304 Suppose you are working on something and you commit it, and then you
305 continue working a bit more, but now you think that what you have in
306 your working tree should be in another branch that has nothing to do
307 with what you committed previously. You can start a new branch and
308 reset it while keeping the changes in your working tree.
309 +
310 ------------
311 $ git tag start
312 $ git switch -c branch1
313 $ edit
314 $ git commit ... <1>
315 $ edit
316 $ git switch -c branch2 <2>
317 $ git reset --keep start <3>
318 ------------
319 +
320 <1> This commits your first edits in `branch1`.
321 <2> In the ideal world, you could have realized that the earlier
322 commit did not belong to the new topic when you created and switched
323 to `branch2` (i.e. `git switch -c branch2 start`), but nobody is
324 perfect.
325 <3> But you can use `reset --keep` to remove the unwanted commit after
326 you switched to `branch2`.
327
328 Split a commit apart into a sequence of commits::
329 +
330 Suppose that you have created lots of logically separate changes and committed
331 them together. Then, later you decide that it might be better to have each
332 logical chunk associated with its own commit. You can use git reset to rewind
333 history without changing the contents of your local files, and then successively
334 use `git add -p` to interactively select which hunks to include into each commit,
335 using `git commit -c` to pre-populate the commit message.
336 +
337 ------------
338 $ git reset -N HEAD^ <1>
339 $ git add -p <2>
340 $ git diff --cached <3>
341 $ git commit -c HEAD@{1} <4>
342 ... <5>
343 $ git add ... <6>
344 $ git diff --cached <7>
345 $ git commit ... <8>
346 ------------
347 +
348 <1> First, reset the history back one commit so that we remove the original
349 commit, but leave the working tree with all the changes. The -N ensures
350 that any new files added with `HEAD` are still marked so that `git add -p`
351 will find them.
352 <2> Next, we interactively select diff hunks to add using the `git add -p`
353 facility. This will ask you about each diff hunk in sequence and you can
354 use simple commands such as "yes, include this", "No don't include this"
355 or even the very powerful "edit" facility.
356 <3> Once satisfied with the hunks you want to include, you should verify what
357 has been prepared for the first commit by using `git diff --cached`. This
358 shows all the changes that have been moved into the index and are about
359 to be committed.
360 <4> Next, commit the changes stored in the index. The `-c` option specifies to
361 pre-populate the commit message from the original message that you started
362 with in the first commit. This is helpful to avoid retyping it. The
363 `HEAD@{1}` is a special notation for the commit that `HEAD` used to be at
364 prior to the original reset commit (1 change ago).
365 See linkgit:git-reflog[1] for more details. You may also use any other
366 valid commit reference.
367 <5> You can repeat steps 2-4 multiple times to break the original code into
368 any number of commits.
369 <6> Now you've split out many of the changes into their own commits, and might
370 no longer use the patch mode of `git add`, in order to select all remaining
371 uncommitted changes.
372 <7> Once again, check to verify that you've included what you want to. You may
373 also wish to verify that git diff doesn't show any remaining changes to be
374 committed later.
375 <8> And finally create the final commit.
376
377
378 DISCUSSION
379 ----------
380
381 The tables below show what happens when running:
382
383 ----------
384 git reset --option target
385 ----------
386
387 to reset the `HEAD` to another commit (`target`) with the different
388 reset options depending on the state of the files.
389
390 In these tables, `A`, `B`, `C` and `D` are some different states of a
391 file. For example, the first line of the first table means that if a
392 file is in state `A` in the working tree, in state `B` in the index, in
393 state `C` in `HEAD` and in state `D` in the target, then `git reset --soft
394 target` will leave the file in the working tree in state `A` and in the
395 index in state `B`. It resets (i.e. moves) the `HEAD` (i.e. the tip of
396 the current branch, if you are on one) to `target` (which has the file
397 in state `D`).
398
399 ....
400 working index HEAD target working index HEAD
401 ----------------------------------------------------
402 A B C D --soft A B D
403 --mixed A D D
404 --hard D D D
405 --merge (disallowed)
406 --keep (disallowed)
407 ....
408
409 ....
410 working index HEAD target working index HEAD
411 ----------------------------------------------------
412 A B C C --soft A B C
413 --mixed A C C
414 --hard C C C
415 --merge (disallowed)
416 --keep A C C
417 ....
418
419 ....
420 working index HEAD target working index HEAD
421 ----------------------------------------------------
422 B B C D --soft B B D
423 --mixed B D D
424 --hard D D D
425 --merge D D D
426 --keep (disallowed)
427 ....
428
429 ....
430 working index HEAD target working index HEAD
431 ----------------------------------------------------
432 B B C C --soft B B C
433 --mixed B C C
434 --hard C C C
435 --merge C C C
436 --keep B C C
437 ....
438
439 ....
440 working index HEAD target working index HEAD
441 ----------------------------------------------------
442 B C C D --soft B C D
443 --mixed B D D
444 --hard D D D
445 --merge (disallowed)
446 --keep (disallowed)
447 ....
448
449 ....
450 working index HEAD target working index HEAD
451 ----------------------------------------------------
452 B C C C --soft B C C
453 --mixed B C C
454 --hard C C C
455 --merge B C C
456 --keep B C C
457 ....
458
459 `reset --merge` is meant to be used when resetting out of a conflicted
460 merge. Any mergy operation guarantees that the working tree file that is
461 involved in the merge does not have a local change with respect to the index
462 before it starts, and that it writes the result out to the working tree. So if
463 we see some difference between the index and the target and also
464 between the index and the working tree, then it means that we are not
465 resetting out from a state that a mergy operation left after failing
466 with a conflict. That is why we disallow `--merge` option in this case.
467
468 `reset --keep` is meant to be used when removing some of the last
469 commits in the current branch while keeping changes in the working
470 tree. If there could be conflicts between the changes in the commit we
471 want to remove and the changes in the working tree we want to keep,
472 the reset is disallowed. That's why it is disallowed if there are both
473 changes between the working tree and `HEAD`, and between `HEAD` and the
474 target. To be safe, it is also disallowed when there are unmerged
475 entries.
476
477 The following tables show what happens when there are unmerged
478 entries:
479
480 ....
481 working index HEAD target working index HEAD
482 ----------------------------------------------------
483 X U A B --soft (disallowed)
484 --mixed X B B
485 --hard B B B
486 --merge B B B
487 --keep (disallowed)
488 ....
489
490 ....
491 working index HEAD target working index HEAD
492 ----------------------------------------------------
493 X U A A --soft (disallowed)
494 --mixed X A A
495 --hard A A A
496 --merge A A A
497 --keep (disallowed)
498 ....
499
500 `X` means any state and `U` means an unmerged index.
501
502 GIT
503 ---
504 Part of the linkgit:git[1] suite