Git reset¶
Git reset is a command used to reset the current HEAD to a specified state, effectively moving the branch pointer to a different commit^[600-developer-tools-git-git-command.md].
Common Use Cases¶
The git reset command is versatile and serves several purposes in workflow management:
- Reverting to a clean state: To discard all local modifications and return the working directory to the state of the most recent commit^[600-developer-tools-git-git-command.md].
- Undoing commits: To move the branch pointer backwards to a previous commit ID^[600-developer-tools-git-git-command.md].
- Canceling merges: To abort a merge operation and return to the state before the merge began^[600-developer-tools-git-git-command.md].
- Aligning with remote: To force the local repository to match a remote branch exactly^[600-developer-tools-git-git-command.md].
Modes of Operation¶
The behavior of git reset changes based on the flags used, determining how the working directory and staging area are affected.
Hard Reset¶
The --hard flag performs a complete reset. It resets the index (staging area) and the working directory to match the specified commit. All changes since that commit are permanently discarded^[600-developer-tools-git-git-command.md].
This is commonly used for:
* Cleaning a messy directory: git reset --hard^[600-developer-tools-git-git-command.md].
* Resetting to a specific previous version: git reset --hard [commit_id]^[600-developer-tools-git-git-command.md].
* Forcefully syncing with a remote branch: git reset --hard origin/master^[600-developer-tools-git-git-command.md].
* Aborting a merge: git reset --hard ORIG_HEAD^[600-developer-tools-git-git-command.md].
Soft Reset¶
The --soft flag resets the HEAD pointer but preserves all changes in the working directory and the staging area. This is useful for undoing a commit while keeping the work ready to be re-committed^[600-developer-tools-git-git-command.md].
For example, to cancel the last commit but retain the modified files:
git reset HEAD^ --soft
Related Commands¶
- Git rebase
- [[Git checkout]]
- [[Git merge]]
Sources¶
^[600-developer-tools-git-git-command.md]