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
Syntax
git reset [--soft|--mixed|--hard] [COMMIT]
Parameters
| Parameter | Description |
|---|---|
--soft |
Keep changes staged. |
--mixed |
Keep changes unstaged (default). |
--hard |
Discard changes (dangerous). |
HEAD~1 |
One commit back. |
Examples
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.
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.
Related
git revert· Create a new commit that undoes an earlier commit's changes; the safe way to undo commits that were already pushed.git reflog· Show where HEAD and branches have pointed recently, so you can recover commits after a reset or bad rebase.git restore· Discard working-tree changes to files, or unstage files, without touching other work.- Undoing Changes · tutorial