2017-01-09 59 views
1

我當時在一家分公司工作,偶然我做了git reset HEAD~1,因爲我以爲我正在重新設置我的上次提交。如何恢復Git軟復位?

問題是,我甚至沒有提交我的更改,所以我已經完成重置以提交由其他人完成的更改。這個提交中的很多變化都是在我正在編寫的文件上完成的,所以我沒有注意到並繼續我的工作。在我提交併推送了更改後,我注意到缺少提交。

develop branch: commitA -> commitB -> commitC 

my branch: commitA -> commitB -> myCommit 

有什麼辦法恢復這些變化,並插入commitC我承諾過嗎?

+0

'git的承諾-A -s -c ORIG_HEAD',但不包括添加文件,你必須運行'git ad d ... <文件列表> ...'事先。 – 0andriy

回答

2

您可以通過git reflog查看您的工作樹。首先回到commitC並挑選你的myCommit頂部git log疊加。然後只需更新遠程。

$ git reflog 
# copy the commit-hash of 'commitC' 

$ git checkout <commitC-hash> 
$ git reflog 
# copy the 'myCommit-hash' 

$ git cherry-pick <myCommit-hash>   # take the 'commitC' top 
$ git checkout -b 'new-my-branch'   # create 'new-my-branch' from current stage 

# Replace 'my-branch' with 'new-my-branch' 
$ git branch -D my-branch     # delete 'my-branch' 
$ git checkout -b my-branch    # create new 'my-branch' from 'new-y-branch' 
$ git push -f origin HEAD     # replace remote/my-branch by local/my-branch 
+0

這遠離您可能提供的簡單解決方案。 – 0andriy

0

你並不需要恢復你的承諾,因爲你可以對遠程一個底墊您當地的分行,並在commitC重新應用你的承諾最重要的是乾淨才帶來。

我並不確切地知道你在本地做了什麼與分支圖,你給我們結束了,但我認爲,你可以簡單地衍合分支上刪除developcommitC拉,然後重播您myCommit在它的上面:

git fetch origin   # bring origin/develop up to date 
git checkout develop  # checkout local develop branch 
git rebase origin/develop # rebase your local branch on the remote one 

現在,你應該能夠通過簡單地快進的遠程分支:

git push origin develop 
+0

太複雜了。 'git rebase --onto origin/develop HEAD〜1' – 0andriy

+0

@ 0andriy你說我的答案太複雜了,當接收upvotes的人更復雜。另外,我不認爲我們需要'rebase - 在這裏。 –