Skip to content

git revert

Undoing 提交 safely 使用 git revert — 创建 inverse 提交不使用 rewriting history.

你将学到

  • 如何 git revert 创建新 提交那个 undoes 上一个 one
  • 为什么 revert 是 safer than reset for shared 分支
  • Reverting 合并提交
  • Handling revert conflicts

概念讲解

What git revert Does

git revert creates a new commit that undoes the changes from a specified commit. Instead of rewriting history, it adds an inverse commit on top.

git revert abc1234

This is the safe way to undo a commit, especially on shared branches. Because it creates a new commit (rather than removing the old one), the history is preserved and other developers who have already pulled are not affected.

revert vs reset

This is a critical distinction:

  • git revert — creates a new commit that undoes the changes. History is preserved. Safe for shared branches.
  • git reset — moves the branch pointer back, removing commits from history. Dangerous for shared branches.
Original history:  A---B---C---D  (main)

After git revert C:  A---B---C---D---C'  (main)
                     (C' is a new commit that undoes C)

After git reset C:   A---B---C  (main)
                     (D is gone from history)

Use revert when others have already seen the commits. Use reset only on local, unpushed commits.

Reverting the Last Commit

git revert HEAD

This opens your editor with a default message like Revert "添加 broken feature". You can edit it and save, or use -m to skip the editor:

git revert HEAD --no-edit

Reverting a Range of Commits

# Revert commits from HEAD~3 to HEAD~1 (inclusive)
git revert HEAD~3..HEAD~1

Each commit in the range gets its own revert commit, applied in order.

Reverting a Merge Commit

Reverting a merge commit is trickier because Git needs to know which parent was the mainline:

git revert -m 1 merge-commit-hash

The -m 1 flag specifies parent 1 (the branch you merged into) as the mainline. This undoes the changes introduced by the merge.

Handling Conflicts

If the code has changed since the commit you are reverting, Git may report a conflict:

git revert abc1234
# CONFLICT (content): Merge conflict in login.js

# Resolve the conflict in your editor, then:
git add login.js
git revert --continue

# Or cancel the revert:
git revert --abort

When to Use revert

  • You pushed a commit that broke the build → git revert
  • You want to undo a commit on a shared branch → git revert
  • You accidentally merged a feature that was not ready → git revert -m 1
  • You need an audit trail of what was undone → git revert (the revert commit records it)

示例代码

              # View recent history to find the commit to revert
git log --oneline -5
# d4c3b2a feat: add payment processing
# a1b2c3d fix: correct login redirect
# f6e5d4c docs: update README

# Revert the payment feature (creates a new undo commit)
git revert d4c3b2a
# Git opens an editor with: "Revert "feat: add payment processing""
# Save and close to complete the revert

# Revert without opening the editor
git revert HEAD --no-edit

# View the result — the revert commit is on top
git log --oneline -3
# xxxxxxx Revert "feat: add payment processing"
# d4c3b2a feat: add payment processing
# a1b2c3d fix: correct login redirect

# Revert a range of commits
git revert HEAD~3..HEAD~1

# Revert a merge commit (specify mainline parent)
git revert -m 1 abc1234

# If a revert causes a conflict, resolve and continue
git revert HEAD
# ... resolve conflict in editor ...
git add conflicted-file.js
git revert --continue

# Cancel a revert that went wrong
git revert --abort
            

Shows reverting 特定提交, reverting HEAD, reverting 不使用编辑消息, reverting 范围, reverting 合并提交使用 -m 1, and handling conflicts. 键 insight 是那个 revert 添加新 提交而不是移除 history.

动手试一试

常见错误

错误写法

Using git reset --困难 on shared 分支相反的 git revert

正确做法

git reset rewrites history, 哪个 breaks things for anyone 谁 already pulled 提交. On shared 分支, 总是 use git revert 到创建新 提交那个 undoes 更改. Reserve git reset for 局部, unpushed 提交仅.

错误写法

Forgetting 那个 revert 创建 NEW 提交, 不移除旧 one

正确做法

git revert 做不 删除原始提交 — 它创建新 提交那个 applies inverse 更改. 原始提交 stays in history. 这个是 intentional: 它 preserves audit trail and 是 safe for shared 分支. 如果你 需要到 移除提交完全, use git reset (locally 仅).

相关速查表

练习 git revert

3 个练习

练习 git revert

查阅不认识的术语

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

查看术语表

动手做项目

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

查看项目

解读错误信息

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

查看错误