]> git.ipfire.org Git - thirdparty/git.git/blame - Documentation/git-reset.txt
Merge branch 'il/maint-xmallocz' into maint
[thirdparty/git.git] / Documentation / git-reset.txt
CommitLineData
215a7ad1
JH
1git-reset(1)
2============
7fc9d69f
JH
3
4NAME
5----
7bd7f280 6git-reset - Reset current HEAD to the specified state
7fc9d69f
JH
7
8SYNOPSIS
9--------
6934dec8 10[verse]
1b5b465f 11'git reset' [--mixed | --soft | --hard | --merge] [-q] [<commit>]
ceb4cacb 12'git reset' [-q] [<commit>] [--] <paths>...
d002ef4d 13'git reset' --patch [<commit>] [--] [<paths>...]
7fc9d69f
JH
14
15DESCRIPTION
16-----------
f67545ea
LAS
17Sets the current head to the specified commit and optionally resets the
18index and working tree to match.
7fc9d69f 19
936a2342
AE
20This command is useful if you notice some small error in a recent
21commit (or set of commits) and want to redo that part without showing
22the undo in the history.
23
24If you want to undo a commit other than the latest on a branch,
5162e697 25linkgit:git-revert[1] is your friend.
936a2342 26
d002ef4d
TR
27The second and third forms with 'paths' and/or --patch are used to
28revert selected paths in the index from a given commit, without moving
29HEAD.
6934dec8
JH
30
31
7fc9d69f
JH
32OPTIONS
33-------
f67545ea 34--mixed::
abda1ef5 35 Resets the index but not the working tree (i.e., the changed files
936a2342
AE
36 are preserved but not marked for commit) and reports what has not
37 been updated. This is the default action.
f67545ea
LAS
38
39--soft::
40 Does not touch the index file nor the working tree at all, but
936a2342 41 requires them to be in a good order. This leaves all your changed
ba020ef5 42 files "Changes to be committed", as 'git-status' would
936a2342 43 put it.
7fc9d69f 44
f67545ea
LAS
45--hard::
46 Matches the working tree and index to that of the tree being
936a2342 47 switched to. Any changes to tracked files in the working tree
6934dec8 48 since <commit> are lost.
7fc9d69f 49
1b5b465f
JH
50--merge::
51 Resets the index to match the tree recorded by the named commit,
52 and updates the files that are different between the named commit
53 and the current commit in the working tree.
54
d002ef4d
TR
55-p::
56--patch::
57 Interactively select hunks in the difference between the index
58 and <commit> (defaults to HEAD). The chosen hunks are applied
59 in reverse to the index.
60+
61This means that `git reset -p` is the opposite of `git add -p` (see
62linkgit:git-add[1]).
63
521b53e5
GP
64-q::
65 Be quiet, only report errors.
66
6934dec8 67<commit>::
cf9d58e4 68 Commit to make the current HEAD. If not given defaults to HEAD.
7fc9d69f 69
1e2ccd3a 70Examples
2b5f3ed3 71--------
1e2ccd3a
JH
72
73Undo a commit and redo::
74+
75------------
76$ git commit ...
48aeecdc
SE
77$ git reset --soft HEAD^ <1>
78$ edit <2>
79$ git commit -a -c ORIG_HEAD <3>
80------------
81+
1e2ccd3a
JH
82<1> This is most often done when you remembered what you
83just committed is incomplete, or you misspelled your commit
84message, or both. Leaves working tree as it was before "reset".
434e6ef8 85<2> Make corrections to working tree files.
1e2ccd3a
JH
86<3> "reset" copies the old head to .git/ORIG_HEAD; redo the
87commit by starting with its log message. If you do not need to
88edit the message further, you can give -C option instead.
41728d69 89+
5162e697 90See also the --amend option to linkgit:git-commit[1].
1e2ccd3a
JH
91
92Undo commits permanently::
93+
94------------
95$ git commit ...
48aeecdc
SE
96$ git reset --hard HEAD~3 <1>
97------------
98+
1e2ccd3a
JH
99<1> The last three commits (HEAD, HEAD^, and HEAD~2) were bad
100and you do not want to ever see them again. Do *not* do this if
97c33c65
TR
101you have already given these commits to somebody else. (See the
102"RECOVERING FROM UPSTREAM REBASE" section in linkgit:git-rebase[1] for
103the implications of doing so.)
1e2ccd3a
JH
104
105Undo a commit, making it a topic branch::
106+
107------------
48aeecdc
SE
108$ git branch topic/wip <1>
109$ git reset --hard HEAD~3 <2>
110$ git checkout topic/wip <3>
111------------
112+
1e2ccd3a
JH
113<1> You have made some commits, but realize they were premature
114to be in the "master" branch. You want to continue polishing
115them in a topic branch, so create "topic/wip" branch off of the
116current HEAD.
117<2> Rewind the master branch to get rid of those three commits.
118<3> Switch to "topic/wip" branch and keep working.
1e2ccd3a 119
d7f078b8 120Undo add::
1e2ccd3a
JH
121+
122------------
48aeecdc 123$ edit <1>
d7f078b8 124$ git add frotz.c filfre.c
48aeecdc
SE
125$ mailx <2>
126$ git reset <3>
127$ git pull git://info.example.com/ nitfol <4>
128------------
129+
434e6ef8 130<1> You are happily working on something, and find the changes
1e2ccd3a
JH
131in these files are in good order. You do not want to see them
132when you run "git diff", because you plan to work on other files
133and changes with these files are distracting.
434e6ef8
SH
134<2> Somebody asks you to pull, and the changes sounds worthy of merging.
135<3> However, you already dirtied the index (i.e. your index does
1e2ccd3a
JH
136not match the HEAD commit). But you know the pull you are going
137to make does not affect frotz.c nor filfre.c, so you revert the
138index changes for these two files. Your changes in working tree
139remain there.
434e6ef8 140<4> Then you can pull and merge, leaving frotz.c and filfre.c
1e2ccd3a 141changes still in the working tree.
1e2ccd3a 142
3ae854c3
JH
143Undo a merge or pull::
144+
145------------
48aeecdc 146$ git pull <1>
3ae854c3
JH
147Auto-merging nitfol
148CONFLICT (content): Merge conflict in nitfol
ec9f0ea3 149Automatic merge failed; fix conflicts and then commit the result.
48aeecdc
SE
150$ git reset --hard <2>
151$ git pull . topic/branch <3>
152Updating from 41223... to 13134...
a75d7b54 153Fast-forward
48aeecdc
SE
154$ git reset --hard ORIG_HEAD <4>
155------------
156+
434e6ef8 157<1> Try to update from the upstream resulted in a lot of
3ae854c3
JH
158conflicts; you were not ready to spend a lot of time merging
159right now, so you decide to do that later.
160<2> "pull" has not made merge commit, so "git reset --hard"
161which is a synonym for "git reset --hard HEAD" clears the mess
162from the index file and the working tree.
434e6ef8 163<3> Merge a topic branch into the current branch, which resulted
a75d7b54 164in a fast-forward.
434e6ef8 165<4> But you decided that the topic branch is not ready for public
3ae854c3
JH
166consumption yet. "pull" or "merge" always leaves the original
167tip of the current branch in ORIG_HEAD, so resetting hard to it
168brings your index file and the working tree back to that state,
169and resets the tip of the branch to that commit.
1e2ccd3a 170
1b5b465f
JH
171Undo a merge or pull inside a dirty work tree::
172+
173------------
174$ git pull <1>
175Auto-merging nitfol
176Merge made by recursive.
177 nitfol | 20 +++++----
178 ...
179$ git reset --merge ORIG_HEAD <2>
180------------
181+
182<1> Even if you may have local modifications in your
183working tree, you can safely say "git pull" when you know
184that the change in the other branch does not overlap with
185them.
186<2> After inspecting the result of the merge, you may find
187that the change in the other branch is unsatisfactory. Running
188"git reset --hard ORIG_HEAD" will let you go back to where you
189were, but it will discard your local changes, which you do not
190want. "git reset --merge" keeps your local changes.
191
192
a0dfb48a
JH
193Interrupted workflow::
194+
8278ac2f
BF
195Suppose you are interrupted by an urgent fix request while you
196are in the middle of a large change. The files in your
a0dfb48a
JH
197working tree are not in any shape to be committed yet, but you
198need to get to the other branch for a quick bugfix.
199+
200------------
201$ git checkout feature ;# you were working in "feature" branch and
202$ work work work ;# got interrupted
d336fc09 203$ git commit -a -m "snapshot WIP" <1>
a0dfb48a
JH
204$ git checkout master
205$ fix fix fix
206$ git commit ;# commit with real log
207$ git checkout feature
48aeecdc
SE
208$ git reset --soft HEAD^ ;# go back to WIP state <2>
209$ git reset <3>
210------------
211+
a0dfb48a 212<1> This commit will get blown away so a throw-away log message is OK.
8278ac2f
BF
213<2> This removes the 'WIP' commit from the commit history, and sets
214 your working tree to the state just before you made that snapshot.
48aeecdc
SE
215<3> At this point the index file still has all the WIP changes you
216 committed as 'snapshot WIP'. This updates the index to show your
217 WIP files as uncommitted.
53682f0c
MH
218+
219See also linkgit:git-stash[1].
a0dfb48a 220
965053b0
PB
221Reset a single file in the index::
222+
223Suppose you have added a file to your index, but later decide you do not
224want to add it to your commit. You can remove the file from the index
225while keeping your changes with git reset.
226+
227------------
228$ git reset -- frotz.c <1>
229$ git commit -m "Commit files in index" <2>
230$ git add frotz.c <3>
231------------
232+
233<1> This removes the file from the index while keeping it in the working
234 directory.
235<2> This commits all other changes in the index.
236<3> Adds the file to the index again.
237
7fc9d69f
JH
238Author
239------
59eb68aa 240Written by Junio C Hamano <gitster@pobox.com> and Linus Torvalds <torvalds@osdl.org>
7fc9d69f
JH
241
242Documentation
243--------------
244Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
245
246GIT
247---
9e1f0a85 248Part of the linkgit:git[1] suite