git reset –hard
Syntax
git reset --hard <commit>
Uses of git reset –hard
- Full Reset: Resets the HEAD, staging area, and working directory to match the specified commit.
- Data Loss: Completely discards all changes in the staging area and working directory, making it impossible to recover them through Git.
- Clean State: Useful for discarding unwanted changes and restoring the repository to a known state.
Example
Given a commit history like this:
A -- B -- C (HEAD)
Running the command:
git reset --hard B
The result is:
- HEAD points to commit B.
- Staging area matches commit B.
- Working directory matches commit B.
Use Case
Use –hard when you want to completely discard any changes in the working directory and staging area and revert to a specific commit. This is often used to undo unwanted changes that are no longer needed.
What’s The Difference Between git reset –mixed, –soft, and –hard?
Git is an important tool for version control in software development. Among its numerous commands, git reset is one of the most powerful but also one of the most complex, especially with its different options: –mixed, –soft, and –hard. This article explains these options, their syntax, features, and uses, and provides practical examples to help you understand their differences and use cases.