git - 在不丢失更新的提交的情况下恢复到上次稳定的提交

标签 git

我是 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
...

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

git subtree push --prefix dist origin gh-pages

我收到这个错误:

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.

我检查过,但是 git subtree push 没有 '-f' 标志...:-(

更新 2:我想我自己找到了上一个问题的答案:

git push origin `git subtree split --prefix dist master`:gh-pages --force

可能是解决方案,对吗? (我还没有运行它...:-)。

更新 3:是的,这是强制推送子树的解决方案。

最佳答案

我会这样做:

git checkout -b upgrades
git checkout master
git reset --hard f36c2f5
git push -f

解释:

  • 通过将最新的(错误的)提交保存在一个单独的分支中以备后用
  • checkout 大师
  • 将 master 重置为您提到的提交:这会丢弃以后对 master 的提交 (但“升级”分支仍然有它们)
  • 推送 master,使用“-f”=“force”标志,因为你想覆盖远程以丢弃一些提交(没有“-f”,git 将不允许你这样做)

关于git - 在不丢失更新的提交的情况下恢复到上次稳定的提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26488422/

相关文章:

git - 在 GitLab Pipelines 中,有没有办法在分离 merge 请求时运行管道?

android - 是否可以同时将文件添加到多个 git 仓库?

git - 获取同名的远程分支

python - 无法从远程存储库读取。请确保您拥有正确的访问权限并且存储库存在

linux - 如何设置路径以在终端中运行 Gitea 命令而不考虑目录?

git - 如何处理 git 中的嵌套分支?

ruby-on-rails - ENOTFOUND : getaddrinfo ENOTFOUND api. heroku.com api.heroku.com:443 heroku 创建错误

git - 列出 Git remote 中所有活跃的分支

git - 你如何在某些 Git 存储库中获取 Git 存储库的名称?

asp.net - 如何使用 GIT 和 ex 部署 ASP.NET (mvc) 站点。 beanstalkapp.com 通过 FTP?