git - 重置 --hard git 远程分支到远程 reflog 中的 SHA 但不在本地 repo 中

标签 git github branch reset

有什么方法可以将远程分支指针重置为您知道在远程 reflog 中但在本地存储库中没有的 SHA?

我非常熟悉重置(软/混合/硬)如何在本地存储库上工作,但这对我没有帮助。

这是我要解决的情况。我有一段时间没有进行提取,并且远程主机已经更新了我在本地还没有的 merge 。

我搞砸了,做了一个 git push --force 意思是只更新远程功能分支,但忘记了一个参数,最终强制功能分支和主控到我在本地的修订。

这意味着 master 现在缺少自从我上次获取以来发生的 merge 提交。我们正在使用 github,所以我收到一封电子邮件,告诉我 master 以前所在的 SHA。我还能够转到 url 并看到提交仍然存在(尚未收集垃圾)。

我能够通过重做我丢失的 merge 并再次推出它来“修复”它(最终更改了 SHA,因为它是不同的提交者和时间)。这不是最优的,如果我在 master 上丢失了不止一个 merge 提交,情况可能会更糟。

我想做的只是 reset --hard 远程 master 分支到它应该位于的 SHA。如果没有 SSH 访问存储库,有什么办法可以做到这一点吗?

这是在 github 上,所以我无法直接访问 repo,无法在那里进行 ssh 并直接重置它。

最佳答案

我不认为你可以在不询问 GitHub 的情况下直接访问/重置任何东西
即没有 contacting GitHub support让他们查看您的 GitHub 存储库的 reflog。
他们将能够通过创建新分支或将其重置为现有分支来使丢失的 SHA1 可见。

实际上,repo 所有者可以查询 GitHub Event API
参见“Does github remember commit IDs?”。


OP Ted Naleid确认,提到 this thread (和 this one )作为一个原因:

This is by design.
That is, when you accidentally push secret data, you can rewind your refs on the server.
Even though the objects still live on the server (until they are garbage-collected) nobody will be able to fetch your secret stuff even if they happen to know the SHA1.

Dan Moulding在他的 own answer 的评论中确认了以下内容:

I've done more testing and found that the clone behavior differs depending on the protocol used.

  • Cloning via SSH (and presumably the via the git protocol) behaves as you've described; i.e. unreachable commits are not transferred to the clone.
    Now I've tested it with HTTPS also (using GitHub itself). And it doesn't look good; the clone does not get unreachable objects.

  • Cloning locally, however, all commits -- unreachable or not -- are transferred.

(但是 OP 澄清说,这只是因为对于本地克隆,默认情况下 .git/objects/ 目录下的文件是硬链接(hard link)的,以尽可能节省空间.
git clone --no-hardlinks file:///Users/yourUser/your/full/repo/path repo-clone-name 不会传输无法访问的提交)

关于git - 重置 --hard git 远程分支到远程 reflog 中的 SHA 但不在本地 repo 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11497405/

相关文章:

git - 无法使用 CLI 登录到 heroku

git - 什么样的目录结构对 DVCS(例如 git)有意义?

git - 如何在git中返回到以前的版本

git - 在 bash 脚本中检测当前目录

git - 在 GitHub 上 checkout 丢失的提交,该提交不在任何分支上

git - 手动创建 Git 分支

ios - 在 github 中创建新分支但它不是最新的?

svn - 克隆一个 git-from-svn 存储库并获取所有远程分支

git - 阻止在 GitHub 上推送到 master ?

git - 如何让现有的git分支跟踪远程分支?