0
0
GitHow-ToBeginner · 3 min read

How to Use Git Reset Mixed: Syntax and Examples

Use git reset --mixed <commit> to move the current branch to a specific commit and reset the staging area to match that commit, but keep your working files unchanged. This lets you uncommit changes while keeping them ready to edit or recommit.
📐

Syntax

The git reset --mixed command moves the current branch pointer to the specified commit and resets the staging area (index) to match that commit. Your working directory files remain unchanged, so you can edit or recommit the changes.

Parts explained:

  • git reset: The command to undo commits or changes.
  • --mixed: The mode that resets the index but not the working directory.
  • <commit>: The commit hash or reference to reset to.
bash
git reset --mixed <commit>
💻

Example

This example shows how to undo the last commit but keep the changes staged for editing or recommitting.

bash
git log --oneline -3
# Output example:
# a1b2c3d Fix typo in README
# 9f8e7d6 Add new feature
# 123abcd Initial commit

git reset --mixed HEAD~1

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

Common Pitfalls

Common mistakes when using git reset --mixed include:

  • Confusing --mixed with --hard, which also resets working files and can cause data loss.
  • Resetting to the wrong commit, which can lose track of recent work.
  • Not realizing that changes remain staged and may be committed again unintentionally.

Always check your current commit and use git status after reset to understand the state.

bash
git reset --hard HEAD~1  # This deletes changes in working directory

# Correct safer way:
git reset --mixed HEAD~1  # Keeps changes in working directory and unstaged
📊

Quick Reference

CommandEffect
git reset --soft Moves HEAD to commit, keeps index and working directory unchanged
git reset --mixed Moves HEAD to commit, resets index, keeps working directory unchanged
git reset --hard Moves HEAD to commit, resets index and working directory (dangerous)

Key Takeaways

git reset --mixed moves HEAD and resets the staging area but keeps your files unchanged.
Use it to undo commits while keeping changes ready to edit or recommit.
Check your commit with git log and your status with git status after reset.
Avoid using --hard unless you want to discard changes permanently.
Always specify the correct commit to avoid losing track of work.