2014-02-23 196 views
6

以下是情況:某些提交已在遠程存儲庫中完成。從遠程git存儲庫中恢復未提交的提交

不幸的是,有人做了push -f,這樣沒有遠程分支再引用這些提交。

更糟的是,我沒有在我的本地克隆提交這些提交。

我的目標基本上是恢復這些提交。這些都是一些我試過的東西:

  • git checkout -b recovery <commit_id>:致命的:引用不是一棵樹
  • git push origin <commit_id>:recovery

    error: refs/heads/recovery does not point to a valid object! error: unable to push to unqualified destination: recovery The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref.

The repository is on bitbucket, and the commit is accessible from the web interface, so it hasn't been garbage-collected.

回答

1

的問題是,你的本地庫不具有提交ID 。

git checkout -b recovery <commit_id> 
git push origin <commit_id>:recovery 

這些命令都失敗出於這個原因,我得到一個GitHub庫完全一樣的消息,如果我用一個承諾,不存在於本地ID。

我想答案是簡單地獲取丟失的本地承諾,在這些方式中的一種:遺憾的是沒有這些工作

git fetch origin   #1 
git fetch origin SHA1 #2 
git fetch origin SHA1:refs/remotes/origin/recovery #3 

。根據this answer,似乎#2曾用於Git v1.4。截至目前,它不再工作。

有一個help page for this on GitHub,但沒有答案,你可以使用:它基本上說,如果你有本地提交,你可以從它創建一個分支並推送它。是的,但是如果你本地沒有提交,那麼是什麼?它沒有回答。

我能想到的兩個選項:

  1. 尋找隊友誰擁有這個承諾。使用git log SHA1很容易查看。如果有人擁有它,他們可以用git branch recovery SHA1從它創建一個分支,然後推送它。
  2. 聯繫bitbucket支持。由於他們有提交,在某個地方,他們應該能夠爲你創建一個recovery分支。
3

如果你的提交的哈希值,那麼你可以從到位桶用這樣的鏈接下載源代碼樹:

https://bitbucket.org/ownerName/repositoryName/get/A0B1C2D.zip 

.zip和.tar.gz兩種工作。

這並不完全恢復提交,但至少不會丟失源代碼更改。我用它來簡單地創建另一個提交相同的更改。

+0

Thanks @Wim!這個解決方案爲我工作。 – Sergey

0

有一點小技巧 - 你說你已經打開了web界面的提交。 我的Stash版本有能力在這個提交上創建一個標籤。尋找「No tags [+]」