场景:某个久远的版本出现了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