简体   繁体   English

如何查找和恢复“重写的” git commit?

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

For some reason I have git repo with commits of code which were rewritten to its previous state. 出于某种原因,我在git repo中添加了提交的代码,这些代码已被重写为以前的状态。 Like this: 像这样:

  • initial code was "Hello world!" 最初的代码是“ Hello world!” and it was committed in repo; 它是通过回购提交的;
  • on some day it was changed to "Bye-bye world!" 在某一天,它被更改为“再见的世界!” and it was committed in repo; 它是通过回购提交的;
  • on some day later this code was reverted on HDD to its initial state ("Hello world!") and committed in repo. 一天后,此代码在HDD上恢复为初始状态(“ Hello world!”),并在回购中提交。

I need to find somehow such commits. 我需要找到某种方式的提交。 It can be not sequential commits but the commits which "rewrites" code to its previous state. 它可以不是顺序提交,而可以是将代码“重写”为其先前状态的提交。

How can I do this? 我怎样才能做到这一点?

You can use git bisect . 您可以使用git bisect

git-bisect git-bisect
Find by binary search the change that introduced a bug 通过二进制搜索查找引入错误的更改

gitbisect is a tool to search and fund out in which commit your code was changed. gitbisect是一个搜索和提供资金的工具,更改了提交的代码。 Its very simple to use it and its very powerful as well. 它使用起来非常简单,功能也非常强大。


How does bisect works? 二等分如何工作?

Bisect simply go over the history log and perform a binary search. 双向简单地检查历史记录并执行二进制搜索。

a binary search or half-interval search algorithm finds the position of a specified input value (the search "key") within an array sorted by key value. 二进制搜索或半间隔搜索算法在按键值排序的数组中查找指定输入值(搜索“键”)的位置。

More about Binary search 有关二进制搜索的更多信息


Git bisect sample 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)

How to automate the bisect ? 如何实现bisect自动化?

Adding the run flag which allow you to automate the search. 添加run标志,使您可以自动执行搜索。
The run gets a script which is being executed on each checkout commit. 运行会获得一个脚本,该脚本将在每次签出提交时执行。

git bisect run my_script arguments


Bisect run exit codes: 双向运行退出代码:

Note that the script (my_script in the above example) should exit with code 0 if the current source code is good, and exit with a code between 1 and 127 (inclusive), except 125, if the current source code is bad. 请注意,如果当前源代码良好,则脚本(上例中的my_script)应以代码0退出,而如果当前源代码不佳,则脚本应以1至127(含)之间的代码退出,但125除外。

Any other exit code will abort the bisect process. 任何其他退出代码都将中止二等分过程。

It should be noted that a program that terminates via "exit(-1)" leaves $? 应该注意的是,通过“ exit(-1)”终止的程序会留下$?。 = 255, (see the exit(3) manual page), as the value is chopped with "& 0377". = 255,(请参见exit(3)手册页),因为该值用“&0377”斩波了。

The special exit code 125 should be used when the current source code cannot be tested. 当无法测试当前源代码时,应使用特殊的退出代码125。 If the script exits with this code, the current revision will be skipped 如果脚本使用此代码退出,则当前版本将被跳过

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM