37 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.)

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.

What To Do...
  • Reset staged changes, but don’t erase any changes.
     
    prompt>​ git reset HEAD
     
    Unstaged changes after reset:
     
    M dolt/__init__.py
     
    ...​ to reset just certain file(s) ...
     
    prompt>​ git reset HEAD <file1> [<file2> <and so on>]
  • Completely undo the last commit.

    Warning: Be careful with this command; it overwrites the changes in any files in your working tree.

     
    prompt>​ git checkout HEAD <file or path to reset>
     
    prompt>
  • Completely remove the last three commits.

    Remember, --hard erases any uncommitted changes in your working tree. Git doesn’t know about those, so you can’t get them back.

     
    prompt>​ git reset --hard HEAD^^^
     
    ...​ or ...
     
    prompt>​ git reset --hard HEAD~3
     
    HEAD is now at 5eada39 add basic read-only version of api wrapper
  • Reset last commit and stage the changes.

    This is useful if the commit has a lot changes, and only a few files and/or paths need to be altered before committing the changes again.

     
    prompt>​ git reset --soft HEAD^
     
    prompt>
  • Undo the last change to HEAD.
     
    prompt>​ git reset ORIG_HEAD
     
    Unstaged changes after reset:
     
    M dolt/apis/external.py
     
    ...​ if you want to completely remove any changes ...
     
    prompt>​ git reset --hard ORIG_HEAD
     
    HEAD is now at 99a7a58 fix params_template

Related Tasks

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.221.133