Files
mintlify__docs/git/reset.mdx
2026-02-09 20:46:44 +00:00

382 lines
5.3 KiB
Plaintext

---
title: git reset
description: Reset current HEAD to the specified state
---
Undo commits or unstage files by moving the branch pointer.
## Syntax
```bash
git reset [options] [<commit>]
```
## Description
`git reset` moves the current branch tip to a different commit and optionally modifies the staging area and working directory. It's used to undo commits, unstage files, or discard changes.
## Common usage
Unstage a file:
```bash
git reset filename.txt
```
Undo last commit, keep changes:
```bash
git reset HEAD~1
```
Undo last commit, discard changes:
```bash
git reset --hard HEAD~1
```
## Reset modes
<ParamField path="--soft" type="flag">
Move HEAD only, keep staging area and working directory
```bash
git reset --soft HEAD~1
```
</ParamField>
<ParamField path="--mixed" type="flag">
Move HEAD and update staging area (default)
```bash
git reset --mixed HEAD~1
```
</ParamField>
<ParamField path="--hard" type="flag">
Move HEAD, update staging area and working directory
```bash
git reset --hard HEAD~1
```
</ParamField>
## Examples
### Unstage files
```bash
# Unstage single file
git reset filename.txt
# Unstage all files
git reset
```
### Undo last commit
Keep changes in working directory:
```bash
git reset --soft HEAD~1
```
Unstage changes:
```bash
git reset HEAD~1
# or
git reset --mixed HEAD~1
```
Discard changes completely:
```bash
git reset --hard HEAD~1
```
<Warning>
`--hard` discards all changes. Use with caution.
</Warning>
### Reset to specific commit
```bash
git reset --hard abc123
```
### Reset to remote branch
```bash
git reset --hard origin/main
```
## Understanding reset modes
### --soft
```bash
git reset --soft HEAD~1
```
- Moves branch pointer back
- Keeps changes staged
- Working directory unchanged
- Use to recommit with better message
### --mixed (default)
```bash
git reset HEAD~1
```
- Moves branch pointer back
- Unstages changes
- Working directory unchanged
- Use to redo staging and commit
### --hard
```bash
git reset --hard HEAD~1
```
- Moves branch pointer back
- Clears staging area
- Discards working directory changes
- Use to completely discard commits
## Common workflows
### Undo last commit, recommit
```bash
git reset --soft HEAD~1
git commit -m "Better commit message"
```
### Unstage everything
```bash
git reset
```
### Discard local changes
```bash
git reset --hard HEAD
```
### Go back 3 commits
```bash
git reset --hard HEAD~3
```
### Reset to match remote
```bash
git fetch origin
git reset --hard origin/main
```
## Reset vs Revert
### Use reset when:
- Commits haven't been pushed
- Working on personal branch
- Want to rewrite history
### Use revert when:
- Commits are already pushed
- Working on shared branch
- Want to preserve history
## Examples with commit references
### Relative references
```bash
# Last commit
git reset HEAD~1
# 3 commits back
git reset HEAD~3
# Parent of HEAD
git reset HEAD^
```
### Absolute references
```bash
# Specific commit hash
git reset abc1234
# Branch or tag
git reset origin/main
git reset v1.0
```
## Recovering from reset
If you reset by mistake:
```bash
# View reflog
git reflog
# Find commit before reset
# Reset back to it
git reset --hard HEAD@{1}
```
## Advanced usage
### Reset specific file to commit
```bash
git reset abc123 -- filename.txt
```
### Keep working directory, reset commit
```bash
git reset --soft HEAD~1
```
### Interactive staging reset
```bash
git reset -p
```
Choose which changes to unstage.
## Unstaging patterns
### Unstage one file
```bash
git reset HEAD filename.txt
```
### Unstage directory
```bash
git reset HEAD directory/
```
### Unstage all
```bash
git reset HEAD
```
## Best practices
- Don't reset commits that are pushed
- Use `--soft` to recommit differently
- Use `--mixed` to redo staging
- Use `--hard` only when certain
- Always check `git reflog` before hard reset
- Prefer `git revert` for public branches
- Communicate with team before resetting shared branches
## Troubleshooting
### Accidentally used --hard
Check reflog:
```bash
git reflog
git reset --hard HEAD@{1}
```
### Reset didn't work
Ensure you're on correct branch:
```bash
git branch
```
### Files still modified after reset
You may need clean:
```bash
git clean -fd
```
## Reset vs other commands
### Reset vs Checkout
```bash
# Reset moves branch pointer
git reset HEAD~1
# Checkout moves HEAD pointer only
git checkout HEAD~1
```
### Reset vs Revert
```bash
# Reset removes commits
git reset HEAD~1
# Revert creates new commit
git revert HEAD
```
### Reset vs Restore
```bash
# Reset (old way to unstage)
git reset HEAD file.txt
# Restore (new way, Git 2.23+)
git restore --staged file.txt
```
## Visual representation
```
Before: A - B - C (main)
↑
HEAD
git reset --soft HEAD~1:
A - B - C (not pointed to)
↑
main
HEAD
git reset --mixed HEAD~1:
A - B - C (not pointed to)
↑
main
HEAD
(changes unstaged)
git reset --hard HEAD~1:
A - B
↑
main
HEAD
(commit C gone, changes lost)
```
## Related commands
- [git revert](/git/revert) - Undo commits safely
- [git restore](/git/restore) - Modern way to restore files
- [git checkout](/git/checkout) - Switch branches
- [git reflog](/git/reflog) - View reference history
- [git clean](/git/clean) - Remove untracked files