Phil for Humanity Phil for Humanity
A Guide for the Survival of Humankind and Helping the World, Society, and Yourself.

Git Reset: Reverting Changes

If you made changes to your local Git repository that you want to permanently discard, then you can run this command:

git reset --hard HEAD

This command will only delete your un-staged (not added) and your added (not committed) changes by reverting back to HEAD on your changed files on your local repository. This command will not revert untracked (new) files that were never added or committed, and this command will not revert committed changes since they are already on the local HEAD.

If you wanted to revert the last locally committed change, then you can run this command:

git reset --hard HEAD~

You can even specify how many commits to revert back to.

git reset --hard HEAD~#

Where # is the number of commits to revert back to. For example, "HEAD~3" would revert back to 3 commits. If you reverted more than the local commits, then you can always pull the commits that were already pushed.

If you want to completely revert back to HEAD of the remote (origin) branch, then you can use this command without having to determine how many commits need reverting.

git reset --hard remotes/origin/BRANCH_NAME

Please remember that the "git reset" command can and will delete your changes permanently, so use this command carefully.

by Phil for Humanity
on 20121102

Related Articles
 » Git Diff
 » Git Status
 » Git Merge