如何撤销git最后一次提交操作流程步骤
技术  /  管理员 发布于 2年前   1147
在git中如何撤销最后一次提交
在本文中,我将显示如何在命令行中使用 git 来恢复代码项目中错误的更改 (提交)。
为什么我要这么做?
我正在做我论文中的一个项目:
在一个环境中开发,然后在另一个由多个虚拟机组成的环境中进行测试。所以我所做的每一个重要的改变都可能对项目的功能产生重大影响。
有时我所做的改变和我预期的结果不同。因此我必须看到更改,并分析项目在最后一次提交前后的行为。
如何查看上次提交?
要测试特定的提交,你需要哈希。
要获取哈希,你可以运行 git log,然后你会得到以下输出:
root@debian:/home/debian/test-project# git log
commit <last commit hash>
Author: Isabel Costa <[email protected]>
Date: Sun Feb 4 21:57:40 2018 +0000
<commit message>commit <before last commit hash>
Author: Isabel Costa <[email protected]>
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 <[email protected]>
Date: Sun Feb 4 21:57:40 2018 +0000
<commit message>commit <before last commit hash>
Author: Isabel Costa <[email protected]>
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>
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号