Git 2 reference · Undoing Changes

git reset Command (Git)

Move the current branch to another commit, keeping changes staged (--soft), unstaged (default) or discarding them (--hard).

Git 2

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

git reset [--soft|--mixed|--hard] [COMMIT]

Parameters

ParameterDescription
--soft Keep changes staged.
--mixed Keep changes unstaged (default).
--hard Discard changes (dangerous).
HEAD~1 One commit back.

Examples

Undo a commit, keep the changes staged
git init -q -b main app && cd app
echo '# Shop' > README.md
git add README.md && git commit -q -m 'Initial commit'
echo a > a.txt; git add .; git commit -q -m 'Add a'
git reset --soft HEAD~1
git status -s; git log --oneline

Output

A  a.txt
7f7f133 Initial commit

Run this on your computer — see “Get Started” for setup.

Throw away the last commit
git init -q -b main app && cd app
echo '# Shop' > README.md
git add README.md && git commit -q -m 'Initial commit'
echo junk > junk.txt; git add .; git commit -q -m 'Bad commit'
git reset -q --hard HEAD~1
ls; git log --oneline

Output

README.md
7f7f133 Initial commit

Run this on your computer — see “Get Started” for setup.

Only reset commits you haven't pushed. For shared history, use git revert. Lost commits can usually be found with git reflog.

Was this page helpful?