侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

如何撤销git最后一次提交操作流程步骤

技术  /  管理员 发布于 6个月前   619

在git中如何撤销最后一次提交

在本文中,我将显示如何在命令行中使用 git 来恢复代码项目中错误的更改 (提交)。

为什么我要这么做?

我正在做我论文中的一个项目:

在一个环境中开发,然后在另一个由多个虚拟机组成的环境中进行测试。所以我所做的每一个重要的改变都可能对项目的功能产生重大影响。

有时我所做的改变和我预期的结果不同。因此我必须看到更改,并分析项目在最后一次提交前后的行为。


如何查看上次提交?

要测试特定的提交,你需要哈希。

要获取哈希,你可以运行 git log,然后你会得到以下输出:

root@debian:/home/debian/test-project# git log
commit <last commit hash>
Author: Isabel Costa <example@email.com>
Date:   Sun Feb 4 21:57:40 2018 +0000
<commit message>commit <before last commit hash>
Author: Isabel Costa <example@email.com>
Date:   Sun Feb 4 21:42:26 2018 +0000<commit message>(...)

你还可以运行git log --oneline 来简化输出:

root@debian:/home/debian/test-project# git log --oneline
<last commit hash> <commit message>
cdb76bf Added another feature
d425161 Added one feature(...)

要测试你认为具有最后工作版本的特定提交

(例如:<before last commit hash>),你可以键入以下内容:

git checkout <commit hash>


如何查看最后一次提交?

要测试特定的提交,需要哈希值。

为了获得哈希值,可以运行 git log,然后会得到这个输出:

root@debian:/home/debian/test-project# git log
commit <last commit hash>
Author: Isabel Costa <example@email.com>
Date:   Sun Feb 4 21:57:40 2018 +0000
<commit message>commit <before last commit hash>
Author: Isabel Costa <example@email.com>
Date:   Sun Feb 4 21:42:26 2018 +0000<commit message>(...)

还可以运行gitlog——oneline 来简化输出:

root@debian:/home/debian/test-project# git log --oneline
<last commit hash> <commit message>
cdb76bf Added another feature
d425161 Added one feature(...)


要测试一个特定的提交 (例如:<在最后一次提交之前的哈希值>),它是你认可的最后一个正常工作的版本,你可以输入以下命令:

git checkout <commit hash> ## 本例中是cdb76bf

这将使工作存储库与此确切提交的状态相匹配。

完成此操作后,你将获得以下输出:

root@debian:/home/debian/test-project# git checkout <commit hash>
Note: checking out '<commit hash>'.
You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at <commit hash>... <commit message>

在分析了特定的提交之后,如果你决定保持该提交状态,你可以撤消最后一次提交。


如何撤消此提交?

如果你希望 撤消 / 恢复上次提交,你可以使用从 git log 命令获得的提交哈希执行以下操作:

git revert <commit hash>

ps: 

git官方文档

https://git-scm.com/docs/git-revert

此命令将在消息的开头创建一个带有 “Revert” 字样的新提交。

在此之后,如果你检查你的存储库状态,你会注意到你在之前测试的提交处分离了 HEAD。

root@debian:/home/debian/test-project# git status
HEAD detached at 69d885e
(...)

你不想看到此消息,因此要解决此问题并附上 HEAD 到你的工作存储库,

您应该签出你正在处理的分支:

git checkout <current branch>


总结

如果你想测试之前的提交,只需执行 git checkout <test commit hash>; 然后您可以测试项目的最后一个工作版本。

如果你想恢复最后一次提交,只需执行 git revert <unwanted commit hash>; 然后你可以推送这个新的提交,这会撤销你之前的提交。

要修复分离的头部,请执行 git checkout <current branch>


  • 上一条:
    Laravel的邮件SPF检查器扩展包:Laravel Mail SPF Checker
    下一条:
    windows10+docker编排实现php多版本共存之自用dnmp开发环境分享
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 性能测试工具、HTTP基准测试工具:wrk(0个评论)
    • namesilo域名+香港服务器+acme.sh给网站生成免费ssl证书流程步骤(0个评论)
    • 控制反转IOC设计模式简单的示例代码(0个评论)
    • 来自京东开发者的接口优化的常见方案实战总结(0个评论)
    • 最新访问谷歌Google镜像/学术/搜索_GitHub镜像/下载加速链接2023/2/16持续更新(0个评论)
    • 近期文章
    • 在go语言中使用GoPDF包把html生成PDF文件示例(0个评论)
    • 在go语言中创建和解析(读取)符号链接示例(0个评论)
    • ubuntu 22.04系统中报错:Python 3.6 is no longer supported by the Python core team...解决方式(0个评论)
    • Laravel 10.4版本发布(0个评论)
    • mysql5.7中实现分区表及分区where in查询示例及分区分表对比浅析(0个评论)
    • nginx + vue配置实现同域名下不同路径访问不同项目(0个评论)
    • 在laravel框架中的5个HTTP客户端技巧分享(0个评论)
    • 在go语言中使用FFmpeg库实现PCM音频文件编码为mp3格式文件流程步骤(0个评论)
    • gopacket免安装Pcap实现驱动层流量抓包流程步骤(0个评论)
    • 在laravel项目中实现密码强度验证功能推荐扩展包:password-strength(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客