2015-10-07 19 views
3

我正在清理分支併合並它們。在完成主分支之後,我嘗試結帳其他分支,但它不讓我這樣做,因爲有2個文件將被覆蓋!我用-f來切換分支(我只希望看到那個分支發生了什麼變化)。沒有什麼是在其他分支。所以我沒有看到任何刪除它的危險。之後,我失去了主分支中的所有其他更改。這是發生了什麼!請讓我知道如何恢復主分支中的所有更改?刪除分支後丟失了所有內容 - 沒有提交(工作目錄清理)

[[email protected] core]$ git status 
# On branch master 
# Changes to be committed: 
# (use "git reset HEAD <file>..." to unstage) 
# 
# modified: blabla.cgi 
# deleted: blabla2.cgi 
# modified: blabla3.cgi 
# new file: blabla4.cgi 
# modified: blabla5.cgi 
# 
[[email protected] core]$ git merge otherbranch 
Already up-to-date. 
[[email protected] core]$ git merge master 
Already up-to-date. 
[[email protected] core]$ git checkout otherbranch 
error: Your local changes to the following files would be overwritten by checkout: 
    blabla3.cgi 
    blabla5.cgi 
Please, commit your changes or stash them before you can switch branches. 
Aborting 
[[email protected] core]$ git checkout -f otherbranch 
Switched to branch 'otherbranch' 
[[email protected] core]$ git status 
# On branch otherbranch 
nothing to commit (working directory clean) 
[[email protected] core]$ git checkout otherbranch 
Already on 'otherbranch' 
[[email protected] core]$ git branch -d otherbranch 
error: Cannot delete the branch 'otherbranch' which you are currently on. 
[[email protected] core]$ git checkout master 
Switched to branch 'master' 
[[email protected] core]$ git branch -D otherbranch 
Deleted branch otherbranch (was d3c9c6f). 
[[email protected] core]$ git branch 
* master 
[[email protected] core]$ git status 
# On branch master 
nothing to commit (working directory clean) 

編輯:當Stony suggessted,我運行reflog。這是輸出!

d302fab [email protected]{0}: checkout: moving from otherbranch to master 
d3c9c6f [email protected]{1}: checkout: moving from master to otherbranch 
d302fab [email protected]{2}: checkout: moving from otherbranch to master 
d3c9c6f [email protected]{3}: checkout: moving from otherbranch to otherbranch 
d3c9c6f [email protected]{4}: checkout: moving from master to otherbranch 
d302fab [email protected]{5}: pull: Fast-forward 
f1569af [email protected]{6}: pull: Fast-forward 
d0f72c6 [email protected]{7}: pull: Fast-forward 
4c007c4 [email protected]{8}: pull: Fast-forward 
d3c9c6f [email protected]{9}: checkout: moving from otherbranch to master 
d3c9c6f [email protected]{10}: merge master: Fast-forward 
506d77d [email protected]{11}: checkout: moving from master to otherbranch 
d3c9c6f [email protected]{12}: checkout: moving from otherbranch to master 
506d77d [email protected]{13}: checkout: moving from master to otherbranch 
d3c9c6f [email protected]{14}: checkout: moving from otherbranch to master 
506d77d [email protected]{15}: checkout: moving from master to otherbranch 
d3c9c6f [email protected]{16}: pull: Fast-forward 
72b9fee [email protected]{17}: pull: Fast-forward 
2a7f380 [email protected]{18}: pull: Fast-forward 
506d77d [email protected]{19}: checkout: moving from otherbranch to master 
506d77d [email protected]{20}: checkout: moving from master to otherbranch 
506d77d [email protected]{21}: checkout: moving from otherbranch to master 
506d77d [email protected]{22}: checkout: moving from master to otherbranch 
506d77d [email protected]{23}: commit: (Ticket ####) 
0cb7e3a [email protected]{24}: pull: Fast-forward 
fef5044 [email protected]{25}: pull: Fast-forward 
a92f38f [email protected]{26}: pull: Fast-forward 
1715fc0 [email protected]{27}: pull: Fast-forward 
8cad089 [email protected]{28}: pull: Fast-forward 
ecb5708 [email protected]{29}: pull: Fast-forward 
87fd764 [email protected]{30}: commit: (Ticket ###) 
745c5ad [email protected]{31}: clone: from [email protected]:/opt/git/seqcore.git/ 

我使用HEAD @ {0},HEAD @ {5},HEAD @ {9}創建了幾個分支,但它沒有幫助我。我仍然無法在主或新創建的分支下看到我的舊更改。

+0

您的reflog中的事件與您運行的命令不匹配。例如,reflog應該顯示你檢查'otherbranch'。您是否刪除了您的副本並再次克隆存儲庫? – Schwern

+0

爲了舉例,我改了名字,但忘了在reflog中做。現在它已更新!感謝您指出@Schwern – kalibrain

回答

2

我不清楚是什麼你的意思是當你說你失去了主分支的變化。從你寫的東西中,我看到失去了兩件事。

  1. 未落實的更改爲blabla*.cgi
  2. 該分支otherbranch

第一個消失了。未提交的更改(即使它們已分階段執行)無法恢復。抱歉。登臺地區被git checkout -f覆蓋。 Git警告過你。

$ git checkout otherbranch 
error: Your local changes to the following files would be overwritten by checkout: 
    blabla3.cgi 
    blabla5.cgi 
Please, commit your changes or stash them before you can switch branches. 
Aborting 

Git在單個工作副本中切換分支的方法很難習慣。從中吸取教訓:

  1. 請不要使用git checkout -f有史以來。
  2. 轉換分支之前存儲您的更改,git stash save
  3. Git當它不允許你做某件事時聽。

otherbranch是可恢復的。 Git幫忙告訴你它的ID在哪裏。

$ git branch -D otherbranch 
Deleted branch otherbranch (was d3c9c6f). 

您可以使用該ID重新創建分支,d3c9c6f

git branch otherbranch d3c9c6f 

沒有必要使用git branch -D。您應該首先使用git branch -d。這樣你會知道合併分支實際上是合併的。


還有一個問題:您的reflog與您編寫的命令不匹配。您的推薦日誌應該會顯示您檢查出otherbranch,但它不。這表示您正在執行結賬的存儲庫,並且您執行reflog的存儲庫是不同的存儲庫。這可以解釋爲什麼缺少某些東西。

我猜你會刪除你的工作副本並克隆一個新的存儲庫。如果是這種情況,那麼你可能已經在沒有被推送的主人提交了。他們走了。

+0

謝謝@Schwern指出分支名稱混淆。我在發佈之前更改了它們,但忘記了在reflog中進行。現在它被編輯了。我選擇你的答案作爲課程。我很高興我現在有一個備份,現在它已基本恢復。 – kalibrain

1

您可以嘗試使用reflog。

git reflog

然後,你可以看到引用日誌。使用該ID您可以恢復上次更改。

22490d2 [email protected]{58}: commit: xxxx 
262a092 [email protected]{59}: commit: xx2 
0a168bc [email protected]{60}: commit: xx2 

git checkout -b my_new_branch [email protected]{60}

例如。有了這個,你可以在這一點上創建一個新的分支。

2

如果您正在尋找blabla3.cgiblabla5.cgi的更改,它們將永遠消失(除非您擁有與git無關的備份解決方案)。當你做git checkout -f otherbranch時,他們不見了。你以後做了什麼並不重要。

不幸的是,git reflog這次不能幫助你,因爲它看起來像你從未犯過它們。 git reflog可以幫助你,如果你提交了一個分支,然後刪除該分支。它還可以幫助你,如果你犯的東西,然後亂七八糟犯下了(例如,通過重訂或--amend)

git真的意味着它時,它會警告您

error: Your local changes to the following files would be overwritten by checkout: 
    blabla3.cgi 
    blabla5.cgi 
Please, commit your changes or stash them before you can switch branches. 
Aborting 
+0

我其實並不關心在從主控切換到其他分支時將被覆蓋的文件。我只是不明白爲什麼我在主分支中丟失了我的更改。 – kalibrain

相關問題