在 Git 中“回退版本”有多种场景和操作方式,下面按常见需求给你一个清晰实用的总结。
git log
或简化显示:
git log --oneline
复制你想回退到的 commit id(如 a1b2c3d)
git reset(最常用)git reset --soft <commit_id>
git reset --mixed <commit_id> # 默认
或
git reset <commit_id>
git reset --hard <commit_id>
⚠️ 会删除工作区代码,慎用!
git revert(推荐用于已推送的提交)git revert <commit_id>
回退多个提交:
git revert <commit1> <commit2>
git checkout -b new-branch <commit_id>
git revert <commit_id>
git push
git reset --hard <commit_id>
git push --force
如果你刚用 reset --hard:
git reflog
找到之前的 commit,再:
git reset --hard <commit_id>
| 需求 | 命令 |
|---|---|
| 本地回退,保留代码 | git reset --mixed |
| 本地彻底回退 | git reset --hard |
| 已 push,安全回退 | git revert |
| 找回误删提交 | git reflog |
如果你愿意,可以告诉我:
我可以给你更精确的一条命令 ✅
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。