Table of contents
Overview
The git reset
command is a powerful Git tool used to undo changes in your repository. It modifies your current branch history, and depending on the mode, it can also affect your staging area and working directory.
Graphite users will find git reset
especially useful when cleaning up commits before publishing or when rewriting history in preparation for a stacked PR workflow.
Syntax
git reset [<mode>] [<commit>]
<mode>
can be--soft
,--mixed
(default), or--hard
.<commit>
specifies the commit hash, branch, or reference you want to reset to.
Modes of git reset
1. --soft
- Moves the branch pointer to the target commit.
- Leaves staging area and working directory intact.
- Useful when you want to re-stage and amend commits.
git reset --soft HEAD~1
This resets the branch one commit back but keeps changes staged.
2. --mixed
(default)
- Moves the branch pointer.
- Clears the staging area (unstages changes).
- Leaves the working directory intact.
git reset HEAD~1
This keeps file modifications but unstages them.
3. --hard
- Moves the branch pointer.
- Clears the staging area.
- Discards changes in the working directory.
git reset --hard HEAD~1
This completely removes the last commit and all associated changes.
Caution: This is destructive. Use with care, especially on shared branches.
Using git reset
with Graphite
Graphite’s workflow encourages clean commit stacks for easier code review.
- Use
git reset --soft
if you need to squash or edit a commit before publishing a stack. - Use
git reset --mixed
when you want to reorganize commits without losing your actual code changes. - Avoid
--hard
resets unless you are confident no work will be lost.
When collaborating with Graphite, always reset only local, unpublished commits. If commits are already stacked and pushed, prefer git revert
to avoid rewriting public history.
FAQ
What's the difference between git reset
and git revert
?
git reset
rewrites history, moving the branch pointer backward.git revert
creates a new commit that undoes a previous one, preserving history.- In Graphite, prefer
revert
for published stacks to avoid conflicts.
When should I use --soft
vs --hard
?
- Use
--soft
if you want to re-stage and adjust commits. - Use
--hard
only when you're certain you don't need the changes anymore.
Can I recover from a git reset --hard
?
- Sometimes, yes. You can use
git reflog
to find the discarded commit hash and reset back to it. - But don't rely on this as a safety net—proceed carefully.
How does Graphite affect git reset
?
- In Graphite's stacked workflow, resets are safe for local (unpushed) commits.
- For published stacks, use
amend
orrevert
instead to avoid disrupting reviewers.