git reset
The command that moves the current branch to another commit, and depending on the mode also resets the staging area and working tree.
git reset --hard deletes uncommitted changes that Git has no copy of.
In more detail
--soft only moves the branch, leaving your changes staged. --mixed, the default, also makes the staging area match, leaving the changes in your files. --hard also makes your files match, discarding uncommitted edits for good. Because it moves where a branch points, it rewrites history, so use it only on commits you have not shared.
Example
git reset --soft HEAD~1Related terms
- three areas — Git's model of where a change can be: the working tree on disk, the staging area waiting for the next commit, and the repository of recorded commits.
- git restore — The command that throws away uncommitted changes to a file, or with --staged takes a file back out of the staging area.
- git revert — A Git command that creates a new commit undoing an earlier one, leaving the history intact.
- rewriting history — Replacing existing commits with new ones, for example with amend, rebase or reset, which gives them new IDs.
- reflog — Git's local record of every position HEAD and each branch has pointed at, used to find and recover commits that no branch points at any more.
Taught in
- Undoing: restore, reset, revert And stash — Advanced Git Internals & Distributed Workflows
- Cherry Picking, Reflog, And Getting Work Back — Advanced Git Internals & Distributed Workflows