Undoing and recovery
Understand reset before using it
Know how reset moves a branch and why hard reset can permanently discard uncommitted work.
8 minute lesson
When you reset a branch to a commit, Git moves the branch reference. The mode decides what happens to the index and working tree.
Before any reset, inspect and protect the current state:
git status
git log --oneline --decorate -5
git branch backup-before-reset
The backup branch gives the current commit a name. This makes recovery much easier.
A soft reset moves the branch but leaves the index and working tree unchanged:
git reset --soft HEAD~1
The old commit’s changes now appear staged.
The default mixed reset moves the branch and resets the index, but preserves working-tree files:
git reset HEAD~1
The changes now appear unstaged.
A hard reset moves the branch and makes both the index and tracked working files match the target:
git reset --hard HEAD~1
This can permanently discard uncommitted tracked changes. It is not a first response to a confusing repository.
Reset is best kept to local history. If a commit is shared, prefer git revert so the correction becomes a normal new commit.
Exercise: in a disposable repository, create a backup branch, run a soft reset, inspect status, then recover by switching back to the backup branch. Do not practice --hard with real work.
Lesson completed