Git恢复至某一个提交的状态

场景:某个久远的版本出现了BUG,不得不修复,但又不想牵扯到此后的版本。
不要用还原(revert)或重置(reset),通过checkout commit即可
VS里没找到checkout某个commit的按钮,需要用到git bash
方法:
1、获取commit的SHA1 ID,可以通过git log查看提交记录,也可以通过Git GUI-Repository-Visualize branch’s History查看
2、Git Bash输入git checkout + 刚才复制的sha1 id,执行后会打印“You are in detached HEAD state”,说明HEAD指向了commit,以下为完整的打印信息

$ git checkout 5b802e307fdc65f7429a4d9693be15985791eea7
Note: switching to '5b802e307fdc65f7429a4d9693be15985791eea7'.

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 switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 5b802e3 

此后就可以基于当前commit编译和调试了,如果需要修改代码,继续看下面的步骤

3、基于当前的HEAD创建一个新的分支,输入git switch -c + 新分支名

4、在新分支上更改与commit

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值