0
0
GitHow-ToBeginner · 3 min read

How to Use git reset --soft: Simple Guide

Use git reset --soft <commit> to move the HEAD pointer to a specific commit while keeping your changes staged. This lets you undo commits but keep your files ready to commit again.
📐

Syntax

The git reset --soft command moves the HEAD pointer to a specified commit without changing the working directory or the staging area (index).

  • git reset: The command to move HEAD and optionally update index and working directory.
  • --soft: Option that keeps changes staged (in the index).
  • <commit>: The commit hash or reference to which you want to move HEAD.
bash
git reset --soft <commit>
💻

Example

This example shows how to undo the last commit but keep the changes staged for a new commit.

bash
git log --oneline -3
# Suppose output is:
# a1b2c3d Fix typo
# 9f8e7d6 Add feature
# 123abcd Initial commit

git reset --soft HEAD~1

git status
Output
On branch main Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: file.txt
⚠️

Common Pitfalls

Common mistakes when using git reset --soft include:

  • Using git reset --soft when you want to discard changes; it only moves HEAD and keeps changes staged.
  • Confusing --soft with --mixed or --hard, which affect the staging area and working directory differently.
  • Resetting to a wrong commit, which can cause confusion about project state.

Always check your commit history with git log before resetting.

bash
git reset --soft HEAD~1  # Correct: moves HEAD, keeps staged

git reset --hard HEAD~1  # Dangerous: discards changes permanently
📊

Quick Reference

OptionEffect
--softMoves HEAD, keeps index and working directory unchanged (changes staged)
--mixed (default)Moves HEAD, resets index, keeps working directory (unstaged changes)
--hardMoves HEAD, resets index and working directory (discards changes)

Key Takeaways

Use git reset --soft to move HEAD and keep changes staged for recommit.
It does not change your working files or unstages changes.
Check commit history with git log before resetting.
Do not confuse --soft with --mixed or --hard reset options.
Use --soft to undo commits but keep your work ready to commit again.