Using git reset
The git reset command changes your
repository and working directory to a known state. Specifically,
git reset adjusts the HEAD ref to
a given commit, and by default, updates the index to match that commit.
If desired, git reset can also modify your working
directory to mirror the revision of your project represented by the
given commit.
You might
construe git reset as “destructive”
because it can overwrite and destroy changes in your working directory.
Indeed, data can be lost. Even if you have a backup of your files, you
might not be able to recover your work. However, the whole point of this
command is to establish and recover known states for the
HEAD, index, and working directory.
The git reset command has three main options:
- git reset --soft
commit The
--softchanges theHEADref to point to the given commit. The contents of your index and working directory are left unchanged. This version of the command has the least effect, changing only the state of a symbolic reference so it points to a new commit.- git reset --mixed
commit --mixedchangesHEADto point to the given commit. Your index contents are also modified to align with the tree structure named by commit, but your working directory contents are left unchanged. This version of the command leaves your index as if you had just staged all the changes represented by commit, and it tells you what remains modified in your working directory.Note that
--mixedis the default mode for git reset.- git reset --hard
commit ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access