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