Download Pragmatic Guide to Git
Transcript
R ESETTING S TAGED C HANGES AND C OMMITS 37 114 Resetting Staged Changes and Commits The git reset command lets you change the HEAD—the latest commit your working tree points to—of your repository. It modifies either the staging area or the staging area and working tree. Git’s ability to craft commits exactly like you want means that you sometimes need to undo changes to the changes you staged with git add. You can do that by calling git reset HEAD <file to change>. This is the most common use of the reset and is like Subversion’s svn revert command. Remember not to get the two confused. (For more on git revert, see Task 36, Reverting Commits, on page 112.) You have two options to get rid of changes completely. git checkout HEAD <file(s) or path(s)> is a quick way to undo changes to your staging area and working tree. Be careful with this command, however, because it removes all changes to your working tree. Git doesn’t know about those changes since they’ve never been committed. There’s no way to get those changes back once you run this command. Another command at your disposal is git reset --hard. It is equally destructive to your working tree—any uncommitted changes or staged changes are lost after running it. Running git reset --hard HEAD does the same thing as git checkout HEAD . (with an extra period after HEAD); it just doesn’t require a file or path to work. You can use git reset --hard to remove more than staged changes and changes to your working tree. You can give it any commit ID to tell Git to reset your HEAD to that and move the current branch to that location. For example, if you make two commits and then realize neither should be there, a quick git reset --hard HEAD^^ fixes that. You can use --soft with git reset. It resets the repository to the commit you specify and stages all of those changes. Any changes you have already staged are not affected, nor are the changes in your working tree. Finally, you can use --mixed to reset the working tree without staging any changes. This also unstages any changes that are staged. Report erratum Download from Wow! eBook <www.wowebook.com> this copy is (P1.0 printing, October 2010)