我对 git 很陌生,所以我在犯任何不可恢复的错误之前先询问...:-)
在过去的几天里,我对我的项目做了一些更改,以升级到我使用的一些库的新版本,但我失败了(也为缺少库文档...:-()。
我刚刚决定放弃升级,所以现在我需要恢复到上次稳定提交。
我也确实推送到了远程,但这应该不是问题,因为我目前是该项目的唯一开发人员。
这是我当前的 git 日志:
$ git log --oneline
22c0713 Upgrading to Firebase 1.1
6d5f9f4 Porting customers to angularfire 0.8
fd9db42 Porting to Angularfire 0.8
d728b82 Working out authenticating on authenticated session problems
d511245 Testing authWithOAuthRedirect
abd9849 Porting to firebase 1.1.2
8884b88 Testing loadRemote() with relative path on public repositories
7830eea Testing loadRemote() with relative path on public repositories
f36c2f5 Finished working on I18N
...
Run Code Online (Sandbox Code Playgroud)
f36c2f5 提交(我上面显示的最后一个提交)是最后一个稳定的提交,也是我想要恢复的提交。
我还想避免丢失较新的(坏的)提交,以供将来参考。
对于这项任务最明智的策略是什么?
更新:感谢您的回答,我快到了(稍后将接受......)。还有一个小问题:我还有一个“gh-pages”分支,用于将我的 dist 子文件夹推送到 github gh-pages 临时站点。
现在,之后
git checkout -b my_branch_for_future_reference
git checkout master
git reset --hard f36c2f5
git push -f
Run Code Online (Sandbox Code Playgroud)
正在做
git subtree push --prefix dist origin gh-pages
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
git push using: origin gh-pages
To git@github.com:MYUSER/MYREPO.git
! [rejected] 3febf7c0812441c7379710d0a1f5f1ec26adbd9e -> gh-pages (non-fast-forward)
error: failed to push some refs to 'git@github.com:MYUSER/MYREPO.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Run Code Online (Sandbox Code Playgroud)
我检查过,但 git subtree push 没有“-f”标志...:-(
更新 2:我想我自己找到了上一个问题的答案:
git push origin `git subtree split --prefix dist master`:gh-pages --force
Run Code Online (Sandbox Code Playgroud)
可能是解决方案,对吗?(我还没有运行它......:-)。
更新3:是的,这是强制推送子树的解决方案。
我会这样做:
git checkout -b upgrades
git checkout master
git reset --hard f36c2f5
git push -f
Run Code Online (Sandbox Code Playgroud)
解释: