目录
Use git revert to Undo a Commit
Find the Commit You Want to Revert
Handling Conflicts When Reverting
首页 开发工具 git 如何恢复特定的提交(创建一个撤销更改的新提交)?

如何恢复特定的提交(创建一个撤销更改的新提交)?

Jul 20, 2025 am 01:41 AM
commit Revert

要撤销已推送到仓库的提交但保留历史记录,可使用git revert创建一个新提交来反向应用指定提交的更改。1. 使用git log --oneline查找目标提交的哈希值;2. 执行git revert 或如git revert HEAD~2来撤销特定提交;3. 若存在冲突,手动解决后通过git add标记并运行git revert --continue继续,或用git revert --abort中止;4. 提交信息可编辑确认;5. 对于合并提交,需加-m 1参数。此方法安全适用于共享分支,避免重写历史引发的问题。

How do I revert a specific commit (create a new commit that undoes the changes)?

You’ve made a commit, pushed it to your repo, and now you want to undo it — but keep the history intact by creating a new commit that reverses those changes. This is a common scenario, especially in team environments where rewriting history can cause issues. Here's how to do it right.

Use git revert to Undo a Commit

The easiest and safest way to undo a specific commit while keeping a traceable history is with git revert. This command creates a new commit that applies the reverse of the targeted commit’s changes.

For example:

git revert HEAD~2

This would create a new commit that undoes the changes from the commit two steps back from the latest (HEAD). If you want to revert the most recent commit, just use:

git revert HEAD

When you run this, Git will open your default text editor so you can confirm or edit the commit message for the new revert commit.

Pro tip: If you’re reverting a merge commit, you’ll need to specify the -m option and indicate which parent you want to consider as the mainline (usually 1).

Find the Commit You Want to Revert

Before running git revert, make sure you know the exact commit hash you want to undo. You can find it using:

git log --oneline

This shows a compact list of commits. Copy the hash of the one you want to revert.

If you're working on a branch shared by others, avoid using git reset to "undo" the commit because it rewrites history. That could mess up other people’s work who have based their changes off the original commits.

Handling Conflicts When Reverting

Sometimes, when you try to revert a commit, Git might show conflicts. This usually happens if later changes overlap with the part of the code being reverted.

In that case:

  • Git will pause the revert and mark the conflicting files.
  • Open each conflicted file and resolve the conflict manually.
  • After resolving, stage the file with git add.
  • Then continue the revert process with:
    git revert --continue

If at any point you decide to abort the revert, use:

git revert --abort

Reverts are safe to push to shared branches since they don’t alter existing history — just add a new commit.


So yes, you can definitely undo a specific commit by creating a new one — and git revert is the tool for the job. Just make sure you pick the right commit and handle any conflicts if they come up.基本上就这些。

以上是如何恢复特定的提交(创建一个撤销更改的新提交)?的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

PHP教程
1511
276
MySQL慢查询中的commit慢和binlog中慢事务有什么区别 MySQL慢查询中的commit慢和binlog中慢事务有什么区别 May 30, 2023 am 08:07 AM

一、问题来源在分析性能问题的时候慢查询和binlog慢事务是常用的手段。最近在分析一个慢查询的,发现其中包含了大量的commit语句慢,但是在分析binlog慢事务的时候不能完成匹配。比如这段时间commit的语句可能有1000个,但是慢事务可能只有100个,这个差得也太多了,那么为什么会出现这种现象呢?二、各自的判定方式慢事务对于一个显示提交的(insert)事务通常如下:GTID_LOG_EVENT和XID_EVENT是命令‘COMMIT’发起的时间。

git如何撤销提交的commit git如何撤销提交的commit Jul 24, 2023 pm 01:33 PM

git撤销提交的commit的方法:1、修改最后一次commit的内容,如果发现最后一次提交中有错误的内容,可以使用“git commit --amend”命令来修改;2、撤销某个commit的变更,如果需要完全撤销某个commit及其对应的变更,可以使用“git revert”命令;3、如果需要完全回退到某个commit之前的状态,可以使用“git reset”命令。

PHP项目中遇到无法调用commit的问题怎么办 PHP项目中遇到无法调用commit的问题怎么办 Mar 04, 2024 pm 05:39 PM

当在PHP项目中遇到无法调用commit的问题,可能是由于代码中的错误、权限不足或者版本控制工具配置问题等引起的。在遇到这类问题时,可以通过以下方法来逐步排查和解决:一、检查代码错误首先,需要检查代码中是否有语法错误、逻辑错误或者其他与提交代码相关的错误。例如,在使用Git作为版本控制工具时,可能会因为文件冲突、未暂存文件等问题导致无法提交。因此,可以通过查

如何解决PHP项目中无法调用commit的困扰 如何解决PHP项目中无法调用commit的困扰 Mar 06, 2024 am 08:30 AM

如何解决PHP项目中无法调用commit的困扰在开发PHP项目的过程中,经常会遇到需要调用外部服务或API,并根据返回结果来执行相应操作的情况。然而,有时候在调用commit操作时却遇到了困扰,无法正常执行。本文将详细介绍在PHP项目中解决无法调用commit的问题,并提供具体的代码示例,帮助开发者快速解决这一困扰。问题分析在PHP项目中,当需要调用外部服务

提交和回滚如何工作? 提交和回滚如何工作? Jun 18, 2025 am 12:28 AM

saveschangesmadedinguringingatransaction,anderollbackundoesthem.atransactionisasecasequenceofsqloperationstreateatedAsasingEdasingLeunitToentoensuredaintegrity,后面的castacidproperties.foreforexample,inamoneytrancement,inamoneytranneytransfer,inamoneytransfer,iNameAccountIsdeBitedBitityBittheotheriisnotheriisnothiis notcreties

如何恢复特定的提交(创建一个撤销更改的新提交)? 如何恢复特定的提交(创建一个撤销更改的新提交)? Jul 20, 2025 am 01:41 AM

要撤销已推送到仓库的提交但保留历史记录,可使用gitrevert创建一个新提交来反向应用指定提交的更改。1.使用gitlog--oneline查找目标提交的哈希值;2.执行gitrevert或如gitrevertHEAD~2来撤销特定提交;3.若存在冲突,手动解决后通过gitadd标记并运行gitrevert--continue继续,或用gitrevert--abort中止;4.提交信息可编辑确认;5.对于合并提交,需加-m1参数。此方法安全适用于共享分支,避免重写历史引发的问题。

如何修改以前的git提交消息 如何修改以前的git提交消息 Aug 01, 2025 am 03:34 AM

Toamendthemostrecentcommitmessage,usegitcommit--amend-m"Yournewcommitmessage"ifthecommithasn’tbeenpushed;thisrewritesthelocalcommithistorywiththenewmessage.2.Toeditthemessageinyourdefaulteditor,rungitcommit--amendwithoutthe-mflag,allowingyo

如何从特定的git commit创建新分支 如何从特定的git commit创建新分支 Jul 25, 2025 am 12:45 AM

使用gitlog--oneline获取目标提交哈希;2.执行gitswitch-c直接从该提交创建并切换到新分支,无需先切换当前分支——此方法精准创建基于指定提交的分支,适用于修复旧版本bug或基于稳定点实验,且不影响原分支,完整结束。

See all articles