[英]How to find and restore “rewritten” git commits?
出於某種原因,我在git repo中添加了提交的代碼,這些代碼已被重寫為以前的狀態。 像這樣:
我需要找到某種方式的提交。 它可以不是順序提交,而可以是將代碼“重寫”為其先前狀態的提交。
我怎樣才能做到這一點?
您可以使用git bisect
。
git-bisect
通過二進制搜索查找引入錯誤的更改
gitbisect
是一個搜索和提供資金的工具,更改了提交的代碼。 它使用起來非常簡單,功能也非常強大。
雙向簡單地檢查歷史記錄並執行二進制搜索。
二進制搜索或半間隔搜索算法在按鍵值排序的數組中查找指定輸入值(搜索“鍵”)的位置。
有關二進制搜索的更多信息
// 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.