Reference

Git

Inspect, save, combine and recover work with clear command effects.

Scope and conventions

Modern Git CLI, including switch and restore (Git 2.23+). Independent examples use illustrative paths and branch names. Inspect status and diffs before changing files or history. Commands below are reference material, not executed by this guide.

Undo & recover

Discard an unstaged file edit

Git

restore --worktree restores the working file from the index by default. The unstaged content is overwritten.

Fragment

Git · Discard an unstaged file edit
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
git diff -- src/main.ts
git restore --worktree -- src/main.ts

Effect: working tree changes. Ordinary unstaged content may have no recoverable Git object.

Bring back a file from a specific commit

Git

--source chooses the source snapshot. Without --staged, this restores the working file without updating the index.

Fragment

Git · Bring back a file from a specific commit
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
git restore --source=HEAD~1 --worktree -- src/main.ts
git diff -- src/main.ts

Undo a commit with a new commit

Git

revert records an inverse change while retaining the original history. Later edits may cause conflicts.

Fragment

Git · Undo a commit with a new commit
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
git revert COMMIT_ID

Effect: new commit. Reverting a merge needs a deliberate mainline-parent choice.

Move HEAD while retaining staged content

Git

reset --soft moves the current branch/HEAD to a commit but leaves the index and working tree unchanged.

Fragment

Git · Move HEAD while retaining staged content
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
git reset --soft HEAD~1
git status

Previously committed changes are now staged relative to the earlier HEAD. This rewrites the branch.

Move HEAD and reset the index

Git

The default mixed reset moves HEAD and resets the index to that commit. Working files keep their content.

Fragment

Git · Move HEAD and reset the index
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
git reset --mixed HEAD~1
git diff

Effect: branch and index change; working edits remain. This differs from path-only git reset -- file, which does not move HEAD.

Recognize a destructive reset

Git

reset --hard moves HEAD and resets both the index and tracked working files to the target. Untracked paths obstructing tracked writes may also be removed.

Fragment

Git · Recognize a destructive reset
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
git reset --hard COMMIT_ID

Uncommitted work can be lost. This is not a general cleanup or undo button.

Find a previous ref position

Git

reflog records local ref movements. Inspect an entry and create a rescue branch to keep the commit reachable.

Fragment

Git · Find a previous ref position
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
git reflog
git show 'HEAD@{1}'
git branch rescue 'HEAD@{1}'

Reflogs expire and are local; they are not a backup of arbitrary uncommitted files.