LinuxCommandLibrary

git-reset

Undo commits and changes

TLDR

Unstage everything

$ git reset
copy

Unstage specific file(s)
$ git reset [path/to/file1 path/to/file2 ...]
copy

Interactively unstage portions of a file
$ git reset [[-p|--patch]] [path/to/file]
copy

Undo the last commit, keeping its changes (and any further uncommitted changes) in the filesystem
$ git reset HEAD~
copy

Undo the last two commits, adding their changes to the index, i.e. staged for commit
$ git reset --soft HEAD~2
copy

Discard any uncommitted changes, staged or not (for only unstaged changes, use git checkout)
$ git reset --hard
copy

Reset the repository to a given commit, discarding committed, staged, and uncommitted changes since then
$ git reset --hard [commit]
copy

SYNOPSIS

git reset [--soft|--mixed|--hard|--merge|--keep|-p|--patch] [-q|--quiet] [--] [<commit>] [<pathspec>...]

PARAMETERS

--soft
    Reset HEAD only; keep index and working tree intact.

--mixed
    Reset HEAD and index (default); working tree unchanged.

--hard
    Reset HEAD, index, and working tree; discards changes.

--merge
    Like --hard, but preserves index for conflicted merges.

--keep
    Like --hard, but aborts if local changes overwritten.

-p, --patch
    Interactive: select hunks to reset.

-q, --quiet
    Suppress feedback messages.

-N, --intent-to-add
    Like --mixed for new paths; stages empty entry.

--root
    Reset root commit without tree-ish.

--recurse-submodules
    Reset submodules to match superproject.

DESCRIPTION

git reset repositions the current branch's HEAD to a specified commit and optionally updates the index (staging area) and working tree. It is essential for undoing commits, unstaging changes, or cleaning the working directory.

The command operates in modes controlling what is reset:
- --soft: Moves HEAD only; index and working tree unchanged. Useful for rewording/squashing commits.
- --mixed (default): Resets HEAD and index; working tree unchanged. Unstages files.
- --hard: Resets HEAD, index, and working tree. Discards all changes—use cautiously.
- --merge: Like --hard but fails if unmerged paths exist.
- --keep: Like --hard but fails if local changes would be overwritten.

Without a commit, it unstages specified paths. Interactive mode (-p) allows selective hunks. Powerful but risky for data loss; pair with git stash or git reflog for safety. Common uses: revert mistaken commits, prepare clean rebases.

CAVEATS

--hard permanently discards uncommitted changes; commits recoverable via git reflog but work is lost unless stashed. Avoid on shared branches.

EXAMPLES

git reset --hard HEAD~1: Discard last commit.
git reset --soft HEAD~2: Uncommit last two, keep changes staged.
git reset HEAD file.txt: Unstage file.

RECOVERY

After reset, use git reflog to find previous HEAD; git reset --hard <ref> to restore.

HISTORY

Introduced in Git 1.5.3 (2007) for path resets; modes like --soft/--hard added in 1.6.0 (2008); evolved with --merge/--keep in 1.7.x for safer resets.

SEE ALSO

Copied to clipboard