Git Hard Reset Basics
Understanding Git Hard Reset
Git hard reset is a powerful command in version control that allows developers to manipulate commit history and repository state. Unlike soft reset, a hard reset completely discards changes and moves the repository pointer to a specified commit.
Core Concepts of Hard Reset
Hard reset involves three primary operations:
Operation |
Description |
Impact |
HEAD |
Current commit reference |
Determines repository state |
Working Directory |
Actual file contents |
Directly modified |
Staging Area |
Prepared changes |
Completely cleared |
Basic Hard Reset Syntax
git reset --hard <commit-hash>
Practical Example on Ubuntu 22.04
## Initialize a git repository
mkdir git-reset-demo
cd git-reset-demo
git init
## Create initial commits
echo "First content" > file.txt
git add file.txt
git commit -m "Initial commit"
echo "Second content" >> file.txt
git add file.txt
git commit -m "Second commit"
## Perform hard reset to first commit
git reset --hard HEAD~1
Workflow Visualization
gitGraph
commit id: "Initial Commit"
commit id: "Second Commit"
reset id: "Reset to Initial Commit"
The hard reset command completely removes subsequent commits and reverts the repository to the specified state, making it a destructive operation that should be used cautiously in version control management.