GIT - 5.9 Time travel with reset and reflog

This lesson is about time-travelling through your Git history using git reset and recovering "lost" commits with git reflog. The starting point is a clean repository on master. We modify coucou.html, add a line, stage it with git add ., and modify it again so we have changes in both the index and the working directory at the same time.

The three reset modes

Sometimes we want to rewind HEAD to a previous commit. git reset can do this in three different flavors:

  • --soft — only moves HEAD. The staging area and working directory are left untouched.
  • --mixed (default) — moves HEAD and resets the staging area, but keeps changes in the working directory.
  • --hard — the most destructive: moves HEAD, wipes the staging area and the working directory back to the target commit's state.

We can try each one in turn:

git reset --soft <commit-hash>
git reset --mixed <commit-hash>
git reset --hard <commit-hash>

After --soft, git hist shows HEAD at the chosen commit while all our edits remain staged. After --mixed, the changes are unstaged but still present in the working directory. After --hard, the working directory is empty — every pending change is gone.

Rescuing lost commits with git reflog

A hard reset feels scary because git log stops listing the discarded commits — they look gone forever. But Git keeps a private journal of every move HEAD has ever made:

git reflog

While git log lists commit hashes from the project's history, git reflog lists every action that has ever moved HEAD: commits, resets, checkouts, branch switches, etc. From that list we can pick the hash of the previous tip — say the commit right before the hard reset — and replay it:

git reset --hard <previous-head-hash>

Running git hist shows the history is fully restored. The combo reset + reflog gives total control over time travel inside your repository — and is the standard trick to recover from accidental hard resets.

Summary

This lesson demonstrates how to navigate Git's commit history and undo changes using two powerful commands: git reset and git reflog. Git reset allows you to move HEAD to a previous commit in three different modes—soft, mixed, and hard—each with varying levels of impact on your staging area and working directory. Git reflog tracks all actions performed on your repository, providing a safety net to recover from mistakes and restore previous commits.

Key points

  • git reset has three modes: --soft (preserves staging area and working directory), --mixed (default, moves changes to working directory), and --hard (most destructive, discards all changes)
  • The --soft flag is the least destructive, only moving HEAD to a previous commit while keeping all changes staged and ready to recommit
  • The --hard flag completely discards all changes in both the staging area and working directory, returning to the exact state of the chosen commit
  • git reflog records every action taken in your repository, allowing you to find commit identifiers even after using git reset
  • Using git reset and reflog together gives you complete control over time travel in Git, allowing recovery from mistakes without permanent data loss

FAQ

What is the difference between soft, mixed, and hard reset modes?

Soft reset only moves HEAD and preserves both staging area and working directory changes. Mixed reset (default) moves HEAD and moves staged changes to the working directory. Hard reset is the most destructive, moving HEAD and discarding all changes in both staging area and working directory.

How can I recover commits after using git reset?

Use git reflog to view all actions performed in your repository. It displays commit identifiers for previous states, allowing you to identify and return to any previous point in your history using git reset.

When should I use soft versus hard reset?

Use soft reset when you want to undo a commit but keep all changes ready to recommit. Use hard reset when you want to completely discard changes and return to an earlier state. Soft is safer for adjusting commits; hard is for removing unwanted work entirely.