假设发现最新的 code 版本有 bugs,想回复更新前的版本,你可以查看、複製并强制还原
git reset -- hard 穿梭你提交的版本之间
输入
git log --oneline -n
来查看过去提交的 commit ID。
n 表示想看前 n 次提交数
假设 n=5,输入 git log --oneline -n
,则为
joe-MacBook-Pro:test stevechung$ git log --oneline -5 // 最近 5 次提交记录ba2f0a4 (HEAD -> master, origin/master) test4 //最新提交的记录f3a6683 test1 // 上一个提交的记录4f0f054 test26fefa2d test177f9ec8 reset
或是可以输入:
git reflog
像是
joe-MacBook-Pro:test stevechung$ git reflog // 查看所有讯息版本4f0f054 (HEAD -> master) HEAD@{0}: reset: moving to HEAD~2 // 这是例子一、回复的当前提交记录ba2f0a4 (origin/master) HEAD@{1}: commit: test4 // 假设你要回复到例子一、开头使用的这个版本f3a6683 HEAD@{2}: commit: test14f0f054 (HEAD -> master) HEAD@{3}: commit: test26fefa2d HEAD@{4}: commit: test177f9ec8 HEAD@{5}: commit: reset58f0631 HEAD@{6}: reset: moving to HEAD58f0631 HEAD@{7}: commit (initial): gitreset
来查看完整 commit 讯息和 commit ID。
接着複製要回复的 commit ID,输入:
git reset --hard "commit ID"
将 commit ID 替换为自己要回复的版本,此时本机储存库已经完成旧版本更新。
若要更新远端储存库,输入:
git push -f
此命令为强制上传,忽略旧版本覆盖较新版本的提醒
以上,完成本机和远端储存库更新