如何在不使用git reflog的情况下恢复丢失的提交? [英] How to recover lost commits without using git reflog?

查看:71
本文介绍了如何在不使用git reflog的情况下恢复丢失的提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过某种方式我无意中强行推送了一个分支,但是我没有提取仅存在于远程的提交.

By somehow I accidentally forcefully pushed a branch and I didn't pull commits that only existed in remote.

通常,当发生这种情况时,可以使用git reflog命令将其恢复,但是这次丢失的提交仅在远程存储库中.

Usually, when this happens it can be recovered with the git reflog command but this time the lost commits were only in the remote repo.

我如何恢复仅存在于GitHub存储库中的那些提交?

How I can recover those commits that only existed in the GitHub repository?

推荐答案

使用GitHub API只需几步即可完成:

It can be done with a few steps with the GitHub API:

  1. 转到此URL:https://api.github.com/repos/<user>/<repo>/events

这将返回/显示一个JSON对象,您可以在其中使用CTRL + F搜索丢失的提交,并放置提交消息或作者.

This will return/show a JSON object where you can search for the lost commit using CTRL+F and putting the commit message or maybe the author.

  1. 复制提交的完整SHA,并将其粘贴到以下命令中:

curl -u <user>:<password> -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '{"ref":"refs/heads/recover-commits", "sha":"<full_commit_sha>"}' https://api.github.com/repos/<user>/<repo>/git/refs

字段:

  • <user>:<password>:您的GitHub帐户的用户名和密码.

  • <user>:<password>: Username and password of your GitHub account.

refs/heads/<branch-name>:新分支的ref,必须有两个斜杠,并以"refs"开头.最后一个斜杠之后是分支的名称

refs/heads/<branch-name>: ref of the new branch, must have two slashes and start with "refs". What comes after the last slash is the name of the branch

sha:提交的代码,例如:"efdec9f65bf420b1af91ad1aded915a42c5fa34d"

sha: the sha of the commit, example: "efdec9f65bf420b1af91ad1aded915a42c5fa34d"

<repo>:存储库名称.

它将使用GitHub API发送请求以创建一个新分支,在这种情况下,该分支名为"recover-commits",指向该提交.

It'll send a request using GitHub API to create a new branch named, in this case, 'recover-commits' that points to that commit.

  1. 如果成功,则应返回如下内容:HTTP/1.1 201 Created Server: GitHub.com ...

您可以从那里挑选或合并提交到您的主分支.

From there you can cherry-pick or merge the commits back into your main branch.

来源.

这篇关于如何在不使用git reflog的情况下恢复丢失的提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆