Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 1 | git-reset(1) |
| 2 | ============ |
| 3 | |
| 4 | NAME |
| 5 | ---- |
Junio C Hamano | 0107892 | 2006-03-10 00:31:47 | [diff] [blame] | 6 | git-reset - Reset current HEAD to the specified state |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 7 | |
| 8 | SYNOPSIS |
| 9 | -------- |
| 10 | 'git-reset' [--mixed | --soft | --hard] [<commit-ish>] |
| 11 | |
| 12 | DESCRIPTION |
| 13 | ----------- |
| 14 | Sets the current head to the specified commit and optionally resets the |
| 15 | index and working tree to match. |
| 16 | |
| 17 | This command is useful if you notice some small error in a recent |
| 18 | commit (or set of commits) and want to redo that part without showing |
| 19 | the undo in the history. |
| 20 | |
| 21 | If you want to undo a commit other than the latest on a branch, |
| 22 | gitlink:git-revert[1] is your friend. |
| 23 | |
| 24 | OPTIONS |
| 25 | ------- |
| 26 | --mixed:: |
Junio C Hamano | 341071d | 2006-06-04 07:24:48 | [diff] [blame] | 27 | Resets the index but not the working tree (i.e., the changed files |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 28 | are preserved but not marked for commit) and reports what has not |
| 29 | been updated. This is the default action. |
| 30 | |
| 31 | --soft:: |
| 32 | Does not touch the index file nor the working tree at all, but |
| 33 | requires them to be in a good order. This leaves all your changed |
| 34 | files "Updated but not checked in", as gitlink:git-status[1] would |
| 35 | put it. |
| 36 | |
| 37 | --hard:: |
| 38 | Matches the working tree and index to that of the tree being |
| 39 | switched to. Any changes to tracked files in the working tree |
| 40 | since <commit-ish> are lost. |
| 41 | |
| 42 | <commit-ish>:: |
| 43 | Commit to make the current HEAD. |
| 44 | |
| 45 | Examples |
Junio C Hamano | 1bb569e | 2006-05-05 23:14:25 | [diff] [blame] | 46 | -------- |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 47 | |
| 48 | Undo a commit and redo:: |
| 49 | + |
| 50 | ------------ |
| 51 | $ git commit ... |
Junio C Hamano | c8d88c2 | 2006-04-29 07:02:01 | [diff] [blame] | 52 | $ git reset --soft HEAD^ <1> |
| 53 | $ edit <2> |
| 54 | $ git commit -a -c ORIG_HEAD <3> |
| 55 | ------------ |
| 56 | + |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 57 | <1> This is most often done when you remembered what you |
| 58 | just committed is incomplete, or you misspelled your commit |
| 59 | message, or both. Leaves working tree as it was before "reset". |
| 60 | <2> make corrections to working tree files. |
| 61 | <3> "reset" copies the old head to .git/ORIG_HEAD; redo the |
| 62 | commit by starting with its log message. If you do not need to |
| 63 | edit the message further, you can give -C option instead. |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 64 | |
| 65 | Undo commits permanently:: |
| 66 | + |
| 67 | ------------ |
| 68 | $ git commit ... |
Junio C Hamano | c8d88c2 | 2006-04-29 07:02:01 | [diff] [blame] | 69 | $ git reset --hard HEAD~3 <1> |
| 70 | ------------ |
| 71 | + |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 72 | <1> The last three commits (HEAD, HEAD^, and HEAD~2) were bad |
| 73 | and you do not want to ever see them again. Do *not* do this if |
| 74 | you have already given these commits to somebody else. |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 75 | |
| 76 | Undo a commit, making it a topic branch:: |
| 77 | + |
| 78 | ------------ |
Junio C Hamano | c8d88c2 | 2006-04-29 07:02:01 | [diff] [blame] | 79 | $ git branch topic/wip <1> |
| 80 | $ git reset --hard HEAD~3 <2> |
| 81 | $ git checkout topic/wip <3> |
| 82 | ------------ |
| 83 | + |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 84 | <1> You have made some commits, but realize they were premature |
| 85 | to be in the "master" branch. You want to continue polishing |
| 86 | them in a topic branch, so create "topic/wip" branch off of the |
| 87 | current HEAD. |
| 88 | <2> Rewind the master branch to get rid of those three commits. |
| 89 | <3> Switch to "topic/wip" branch and keep working. |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 90 | |
| 91 | Undo update-index:: |
| 92 | + |
| 93 | ------------ |
Junio C Hamano | c8d88c2 | 2006-04-29 07:02:01 | [diff] [blame] | 94 | $ edit <1> |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 95 | $ git-update-index frotz.c filfre.c |
Junio C Hamano | c8d88c2 | 2006-04-29 07:02:01 | [diff] [blame] | 96 | $ mailx <2> |
| 97 | $ git reset <3> |
| 98 | $ git pull git://info.example.com/ nitfol <4> |
| 99 | ------------ |
| 100 | + |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 101 | <1> you are happily working on something, and find the changes |
| 102 | in these files are in good order. You do not want to see them |
| 103 | when you run "git diff", because you plan to work on other files |
| 104 | and changes with these files are distracting. |
| 105 | <2> somebody asks you to pull, and the changes sounds worthy of merging. |
| 106 | <3> however, you already dirtied the index (i.e. your index does |
| 107 | not match the HEAD commit). But you know the pull you are going |
| 108 | to make does not affect frotz.c nor filfre.c, so you revert the |
| 109 | index changes for these two files. Your changes in working tree |
| 110 | remain there. |
| 111 | <4> then you can pull and merge, leaving frotz.c and filfre.c |
| 112 | changes still in the working tree. |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 113 | |
| 114 | Undo a merge or pull:: |
| 115 | + |
| 116 | ------------ |
Junio C Hamano | c8d88c2 | 2006-04-29 07:02:01 | [diff] [blame] | 117 | $ git pull <1> |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 118 | Trying really trivial in-index merge... |
| 119 | fatal: Merge requires file-level merging |
| 120 | Nope. |
| 121 | ... |
| 122 | Auto-merging nitfol |
| 123 | CONFLICT (content): Merge conflict in nitfol |
| 124 | Automatic merge failed/prevented; fix up by hand |
Junio C Hamano | c8d88c2 | 2006-04-29 07:02:01 | [diff] [blame] | 125 | $ git reset --hard <2> |
| 126 | $ git pull . topic/branch <3> |
| 127 | Updating from 41223... to 13134... |
| 128 | Fast forward |
| 129 | $ git reset --hard ORIG_HEAD <4> |
| 130 | ------------ |
| 131 | + |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 132 | <1> try to update from the upstream resulted in a lot of |
| 133 | conflicts; you were not ready to spend a lot of time merging |
| 134 | right now, so you decide to do that later. |
| 135 | <2> "pull" has not made merge commit, so "git reset --hard" |
| 136 | which is a synonym for "git reset --hard HEAD" clears the mess |
| 137 | from the index file and the working tree. |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 138 | <3> merge a topic branch into the current branch, which resulted |
| 139 | in a fast forward. |
| 140 | <4> but you decided that the topic branch is not ready for public |
| 141 | consumption yet. "pull" or "merge" always leaves the original |
| 142 | tip of the current branch in ORIG_HEAD, so resetting hard to it |
| 143 | brings your index file and the working tree back to that state, |
| 144 | and resets the tip of the branch to that commit. |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 145 | |
Junio C Hamano | 74e8a2d | 2006-01-14 03:58:41 | [diff] [blame] | 146 | Interrupted workflow:: |
| 147 | + |
Junio C Hamano | a6331a8 | 2006-01-22 07:50:33 | [diff] [blame] | 148 | Suppose you are interrupted by an urgent fix request while you |
| 149 | are in the middle of a large change. The files in your |
Junio C Hamano | 74e8a2d | 2006-01-14 03:58:41 | [diff] [blame] | 150 | working tree are not in any shape to be committed yet, but you |
| 151 | need to get to the other branch for a quick bugfix. |
| 152 | + |
| 153 | ------------ |
| 154 | $ git checkout feature ;# you were working in "feature" branch and |
| 155 | $ work work work ;# got interrupted |
Junio C Hamano | c8d88c2 | 2006-04-29 07:02:01 | [diff] [blame] | 156 | $ git commit -a -m 'snapshot WIP' <1> |
Junio C Hamano | 74e8a2d | 2006-01-14 03:58:41 | [diff] [blame] | 157 | $ git checkout master |
| 158 | $ fix fix fix |
| 159 | $ git commit ;# commit with real log |
| 160 | $ git checkout feature |
Junio C Hamano | c8d88c2 | 2006-04-29 07:02:01 | [diff] [blame] | 161 | $ git reset --soft HEAD^ ;# go back to WIP state <2> |
| 162 | $ git reset <3> |
| 163 | ------------ |
| 164 | + |
Junio C Hamano | 74e8a2d | 2006-01-14 03:58:41 | [diff] [blame] | 165 | <1> This commit will get blown away so a throw-away log message is OK. |
Junio C Hamano | a6331a8 | 2006-01-22 07:50:33 | [diff] [blame] | 166 | <2> This removes the 'WIP' commit from the commit history, and sets |
| 167 | your working tree to the state just before you made that snapshot. |
Junio C Hamano | c8d88c2 | 2006-04-29 07:02:01 | [diff] [blame] | 168 | <3> At this point the index file still has all the WIP changes you |
| 169 | committed as 'snapshot WIP'. This updates the index to show your |
| 170 | WIP files as uncommitted. |
Junio C Hamano | 74e8a2d | 2006-01-14 03:58:41 | [diff] [blame] | 171 | |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 172 | Author |
| 173 | ------ |
| 174 | Written by Junio C Hamano <junkio@cox.net> and Linus Torvalds <torvalds@osdl.org> |
| 175 | |
| 176 | Documentation |
| 177 | -------------- |
| 178 | Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>. |
| 179 | |
| 180 | GIT |
| 181 | --- |
| 182 | Part of the gitlink:git[7] suite |