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