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