2013-03-30 256 views
3

假設這樣的:如何在同一個提交的rebase之後恢復合併提交?

mkdir test; cd test 
echo "1" > file1; git init; git add .; git commit -m "initial - file 1" # 1st commit on master 
echo "2" > file2; git add .; git commit -m "file 2"      # 2nd commit on master 
git checkout -b newbranch             # creates newbranch 
echo "1" >> file1; git add .; git commit -m "changed 1"     # 1st commit on newbranch 
git checkout master              # goes to master 
echo "2" >> file2; git add .; git commit -m "changed 2"     # 3rd commit on master 
git merge newbranch -m "merge commit"          # merge newbranch on master 
echo "3" > file3; git add .; git commit --amend -m "merge commit"   # amend merge commit and adds file3 
git rebase HEAD~2               # don't change anything, just leave 
ls                  # there isn't file3 anymore! 

有沒有辦法恢復合併提交,以便進行修改就可以了,這些變化不會丟失?

回答

0

您所做的更改仍在reflog中。

經過意外重建(我認爲這是偶然的,因爲您在談論「丟失」提交),請運行git reflog

在最上面的集合中找到剛剛在最後一個「rebase:」行下面的條目。左側的提交散列是在開始重新綁定之前修改的合併提交。

+0

Thx,但'git reflog'上的任何提交都顯示'file3'。在所有提交中'git whatchanged'。 – bitlogic

+0

@bitlogic再次檢查。運行你的上面的腳本,然後是'checkout HEAD @ {4}',恢復'file3'。 – Borealid

+0

是的,這是正確的。在那。奇怪的是,'git whatchanged'不顯示文件被添加! – bitlogic