您不能修改以前的提交。每個提交都是唯一的。
你可以做的是用新的提交重新創建歷史記錄。使用命令git-rebase
我明白,你是在現在這個點:
Pre_A - A - B
\
A1
你已經失去了你「B」提交。要恢復提交使用git-reflog
git reflog
你會看到分支HEAD
的最新位置。其中之一是B承諾。記下行首的哈希。例如:
git reflog
a123b Ammend Commit of Previous A that generates A1 commit
b234c git reset soft to A Commit
c345d B Commit
b234c A Commit ##Look that the HASH is the same that the git-reset##
一旦您擁有B提交的HASH,創建一個分支來恢復提交。
git branch tmpBranch c345d
在這一點上,你需要的是把這個承諾在「A1」,而不是在「A」是新的歷史。你得到這個使用git-rebase
我假設你在你的主分支工作。
git checkout tmpBranch
git rebase --onto master tmpBranch~1 tmpBranch
這重新創建一個歷史提交落後tmpBranch的承諾,直到tmpBranch承諾(在你的情況下,只有一個承諾,在B犯)在主(即點至A1提交)。
謝謝!我不知道「櫻桃挑選」的命令,非常有用!我下次試試你的其他解決方案。 – Ellda