Skip to content

git reset

Rewriting 局部 history 使用 git reset — soft, mixed, and 困难模式 and 当到 use 每个.

你将学到

  • three reset 模式: --soft, --mixed (默认), and --困难
  • 如何 reset moves 分支指针
  • Unstaging files 使用 git reset 不使用 losing 更改
  • 为什么 reset 是 dangerous on shared 分支

概念讲解

What git reset Does

git reset moves the current branch pointer to a different commit. Depending on the mode, it also affects the staging area and working directory.

git reset HEAD~1

This moves the current branch back one commit. The key question is: what happens to your changes?

The Three Modes

--soft (safest)

git reset --soft HEAD~1

Moves the branch pointer back, but keeps your changes staged. The commit is undone, but all its changes are in the staging area, ready to commit again. Useful if you want to re-commit with a different message or combine commits.

--mixed (default)

git reset HEAD~1
git reset --mixed HEAD~1  # same thing

Moves the branch pointer back and un-stages the changes. The changes are still in your working directory (unstaged), but not in the staging area. This is the default behavior.

--hard (most dangerous)

git reset --hard HEAD~1

Moves the branch pointer back and discards all changes — both staged and in the working directory. The commit and its changes are gone. This cannot be undone (unless you find the commit hash in git reflog).

Visual Summary

Original:  A---B---C  (HEAD, staging, working dir all at C)

reset --soft HEAD~1:   A---B  (HEAD at B, staging has C's changes, working dir unchanged)
reset --mixed HEAD~1:  A---B  (HEAD at B, staging empty, working dir has C's changes)
reset --hard HEAD~1:   A---B  (HEAD at B, staging empty, working dir clean — C's changes LOST)

Unstaging Files

A common, safe use of git reset is to unstage a file without losing the changes:

git add wrong-file.js
git reset wrong-file.js      # unstage the file (changes stay in working dir)
# or:
git restore --staged wrong-file.js

This uses the default --mixed mode and is completely safe — no changes are lost.

Resetting to a Remote Branch

If your local branch has diverged badly from the remote, you can reset to match the remote:

git fetch origin
git reset --hard origin/main

Warning: this discards all local commits and changes. Use only when you are sure you want to match the remote exactly.

Recovering from a Bad Reset

If you accidentally ran git reset --困难 and lost commits, you can often recover using the reflog:

git reflog
# find the hash of the commit you lost
git reset --hard <lost-commit-hash>

The reflog records every HEAD movement, so even "删除" commits are recoverable for a while (until garbage collection runs).

When to Use reset vs revert

  • Local commits not yet pushed → git reset is fine
  • Commits already pushed to a shared branch → use git revert instead
  • Just want to unstage a file → git reset <file> (safe, default mode)
  • Want to discard all local changes → git reset --困难 (destructive)

示例代码

              # --- Safe: unstage a file without losing changes ---
git add wrong-file.js
git reset wrong-file.js        # changes stay in working dir, just unstaged
# or the newer way:
git restore --staged wrong-file.js

# --- Soft reset: undo last commit, keep changes staged ---
git commit -m "oops, bad message"
git reset --soft HEAD~1
# changes are staged, re-commit with a better message:
git commit -m "feat: add user authentication"

# --- Mixed reset (default): undo last commit, unstage changes ---
git reset HEAD~1
# changes are in working dir (unstaged), stage and commit again:
git add .
git commit -m "feat: add user authentication"

# --- Hard reset: discard the last commit entirely (DANGEROUS) ---
git reset --hard HEAD~1
# the commit and its changes are GONE

# --- Reset to match the remote branch ---
git fetch origin
git reset --hard origin/main
# local main now exactly matches remote main

# --- Recover a lost commit with reflog ---
git reflog
# c3d2e1f HEAD@{0}: reset: moving to HEAD~1
# a1b2c3d HEAD@{1}: commit: feat: add payment
git reset --hard a1b2c3d  # recovered!

# --- Reset a specific file to a previous commit ---
git reset abc1234 -- config.js
git checkout -- config.js
            

Shows 所有 three reset 模式, unstaging files safely, resetting 到 remote 分支, recovering lost 提交通过 reflog, and resetting 特定 file. reflog recovery 是 lifesaver 当你 accidentally reset --困难.

动手试一试

  • Code Diff

    检查什么更改 reset 将 discard 之前运行 --困难.

  • File Hash Checksum

    Git tracks 内容通过 hash — reflog uses 这些到 recover 提交.

常见错误

错误写法

运行 git reset --困难 on 提交那个是 already pushed

正确做法

一次提交是 pushed, other developers 可能有 pulled 它们. reset --困难移除它们从 history, causing conflicts for 每个人. Use git revert 相反, 哪个创建新 提交那个 undoes 更改不使用 rewriting history. 仅 use reset --困难 on 局部, unpushed 提交.

错误写法

Using git reset --困难 and losing uncommitted 工作那个是 从不 committed

正确做法

git reset --困难 discards uncommitted 更改使用否 简单 recovery (reflog 仅 tracks 提交, 不 uncommitted 工作). 之前运行 reset --困难, stash 你的更改使用 git stash 如果你 可能需要它们. Or 提交它们首先所以它们是 in reflog.

相关速查表

练习 git reset

9 个练习

练习 git reset

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误