簡體   English   中英

如何查找和恢復“重寫的” git commit?

[英]How to find and restore “rewritten” git commits?

出於某種原因,我在git repo中添加了提交的代碼,這些代碼已被重寫為以前的狀態。 像這樣:

  • 最初的代碼是“ Hello world!” 它是通過回購提交的;
  • 在某一天,它被更改為“再見的世界!” 它是通過回購提交的;
  • 一天后,此代碼在HDD上恢復為初始狀態(“ Hello world!”),並在回購中提交。

我需要找到某種方式的提交。 它可以不是順序提交,而可以是將代碼“重寫”為其先前狀態的提交。

我怎樣才能做到這一點?

您可以使用git bisect

git-bisect
通過二進制搜索查找引入錯誤的更改

gitbisect是一個搜索和提供資金的工具,更改了提交的代碼。 它使用起來非常簡單,功能也非常強大。


二等分如何工作?

雙向簡單地檢查歷史記錄並執行二進制搜索。

二進制搜索或半間隔搜索算法在按鍵值排序的數組中查找指定輸入值(搜索“鍵”)的位置。

有關二進制搜索的更多信息


Git二等分樣本

// Start the bisect operation
git bisect start 

// mark the current as bad or good depends on the content of your given
// file. In your case since you want to find the commit who modified your
// code to "Hello world!" lets assume the current file is the bad state
git bisect bad 

// Assume 100 commits ago it was before your change (or give a sha if you
// know where its was ok last time
git bisect good HEAD~100

// Now git will split the history and start looping over it.
... do your tests and decide if its good or bad (check the file content)

如何實現bisect自動化?

添加run標志,使您可以自動執行搜索。
運行會獲得一個腳本,該腳本將在每次簽出提交時執行。

git bisect run my_script arguments


雙向運行退出代碼:

請注意,如果當前源代碼良好,則腳本(上例中的my_script)應以代碼0退出,而如果當前源代碼不佳,則腳本應以1至127(含)之間的代碼退出,但125除外。

任何其他退出代碼都將中止二等分過程。

應該注意的是,通過“ exit(-1)”終止的程序會留下$?。 = 255,(請參見exit(3)手冊頁),因為該值用“&0377”斬波了。

當無法測試當前源代碼時,應使用特殊的退出代碼125。 如果腳本使用此代碼退出,則當前版本將被跳過

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM