Skip to content

Git Time Machine

A Visual Guide to Undoing Things for Git Beginners

When you commit too early, forget to add a file, or mess up the commit message… don’t worry, you can “fix” it!

The Flow: Replace, Don’t Just Repair

Flawed Commit

Forgot to add forgotten_file

Add Missing File

git add forgotten_file

Perfect Commit

The old commit is replaced

Using the --amend option, Git creates a brand new commit with the contents of your staging area and replaces your last “mistaken” commit with it.

1. Initial commit, but something is missing

shell
git commit -m 'initial commit'

2. Stage the forgotten file

shell
git add forgotten_file'

3. Re-commit using —amend

shell
git commit --amend'
  • Note

    This opens your editor to change the commit message.

    In the end, you only have one, perfect commit.

Understanding Git’s Three Core Areas

Working Directory

Where you actually modify files

Staging Area

Files after git add

Local Repository

History after git commit

The following operations are essentially about moving files between these three areas.

Unstaging a File: From Staging Area to Working Directory

Section titled “Unstaging a File: From Staging Area to Working Directory”

When you use git add * and accidentally stage a file you didn’t mean to, you need to pull it back from the staging area.

Staging Area

`restore —staged’ (Modern Git)

`rest HEAD’ (Older Git)

Working Directory

git status will directly tell you to use git restore.

# Let's say you accidentally staged CONTRIBUTING.md
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
# Run the command it suggests
git restore --staged CONTRIBUTING.md

Older versions of Git will suggest using git reset.

# Same situation, different suggestion
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
# Run the command it suggests
git reset HEAD CONTRIBUTING.md

Discarding Changes: From Repository to Working Directory

Section titled “Discarding Changes: From Repository to Working Directory”

When you realize your changes to a file are completely wrong and want to revert it to how it was in the last commit…

Repository

(Last committed version)

`restore’ (Modern Git)

`checkout —’ (Older Git)

Working Directory ❌

(Last committed version)

# 'status' tells you how to discard changes
Changes not staged for commit:
(use "git restore <file>..." to discard changes in working directory)
# This command discards all local changes to the file
git restore CONTRIBUTING.md
# The suggestion in older Git versions
Changes not staged for commit:
(use "git checkout -- <file>..." to discard changes in working directory)
# This command has the same dangerous effect
git checkout -- CONTRIBUTING.md

Unstage File (reset HEAD /restore —staged): 10 (Very safe);

Amend Local Commit (commit —amend): 25 (Relative safe);

Discard Local Changes (checkout — /restore) : 80 (Dangerous, loses local changes);

Amend Pushed Commit (force push): 100 (Extremely dangerous, breaks remote history);