繁体   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