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

相关推荐

  1. Git恢复一个提交状态

    2024-04-21 19:24:04       34 阅读
  2. 6. git 指定一个提交

    2024-04-21 19:24:04       43 阅读
  3. Git一个仓库合并另一个仓库一次提交

    2024-04-21 19:24:04       48 阅读
  4. Git 中把文件恢复到之前提交

    2024-04-21 19:24:04       26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-21 19:24:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-21 19:24:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-21 19:24:04       82 阅读
  4. Python语言-面向对象

    2024-04-21 19:24:04       91 阅读

热门阅读

  1. 【大模型系列】目录

    2024-04-21 19:24:04       35 阅读
  2. GoLang核心知识点

    2024-04-21 19:24:04       36 阅读
  3. Angular自定义异步表单验证

    2024-04-21 19:24:04       33 阅读
  4. Scala之面向对象 Day -3

    2024-04-21 19:24:04       44 阅读
  5. 134. 加油站

    2024-04-21 19:24:04       30 阅读
  6. 【设计模式】享元模式

    2024-04-21 19:24:04       40 阅读
  7. 批量控制教程-Ansible管理windows

    2024-04-21 19:24:04       41 阅读
  8. 【Ansible】02

    2024-04-21 19:24:04       35 阅读
  9. linux的工具tcptrace的输出信息的解释和介绍

    2024-04-21 19:24:04       38 阅读
  10. 【设计模式】7、decorate 装饰模式

    2024-04-21 19:24:04       34 阅读
  11. 黑马点评项目遇到的部分问题

    2024-04-21 19:24:04       124 阅读