如何撤销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>
博主 在
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..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号