1、回退代码到某个版本
场景:
提交顺序:commit-a --> commit-b --> commit-c
需要回到 commit-b 提交后的版本,丢弃后续的提交(commit-c)
解决步骤:
# 1.使用git log 查看历史提交
git log
# 2.找到需要回退版本的 commitId
如 0bea65aa
# 3.回退本地代码库
git reset --hard 0bea65aa
# 4.推送到远程
git push -f origin master
2、撤销某次merge、commit
场景:
提交顺序:commit-a --> commit-b --> commit-c
需要撤销 commit-b 提交,保留后续的提交(commit-c)
解决步骤:
# 1.使用git log 查看历史提交
git log
# 2.找到需要回退版本的 commitId
如 0bea65aa
# 3.撤销本次提交
git revert -m 1 0bea65aa
# 4.提交本次撤销
git commit -m 'revert'
本文详细介绍了如何使用Git进行代码回退和撤销特定提交。在场景一中,通过`git log`找到commit-id,使用`git reset --hard commit-id`回退到指定版本,再用`git push --force origin master`推送更改。在场景二中,要撤销某次merge或commit,先用`git log`获取commit-id,然后运行`git revert -m 1 commit-id`,接着提交这次撤销操作。这些步骤对于日常开发中修正错误或管理代码历史至关重要。
3万+

被折叠的 条评论
为什么被折叠?



