查找一段代码何时从 git 存储库中删除

Finding when a section of code was removed from a git repository

我在旧版本软件中有一段代码已从当前版本中删除。我想找出此部分何时从代码库中删除。目前我这样做的方法是手动将我的集成分支与每个旧提交进行比较,但是在包含代码的文件版本和我当前的版本之间有很多提交。

所以简而言之,如果我在旧版本文件的第 44-52 行有代码,而新版本中不存在该代码,我如何找到删除后的提交?

做你想做的事情的方法是使用 bisect run <script>.
像目前一样使用二分法,并将二分法与脚本选项一起使用。

该脚本将 return 用于跳过的适当代码(或 125 表示不可测试 - 更适合您的情况)。

Note that the script 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.

Any other exit code will abort the bisect process. It should be noted that a program that terminates via "exit(-1)" leaves $? = 255, (see the exit(3) manual page), as the value is chopped with "& 0377".

The special exit code 125 should be used when the current source code cannot be tested. If the script exits with this code, the current revision will be skipped (see git bisect skip above).

125 was chosen as the highest sensible value to use for this purpose, because 126 and 127 are used by POSIX shells to signal specific error status (127 is for command not found, 126 is for command found but not executable---these details do not matter, as they are normal errors in the script, as far as bisect run is concerned).


演示代码

Here 您可以查看有关如何使用 git bisect

的示例代码

尝试git log -S"<code>" -- file_paths,它显示指定字符串发生更改的提交。

这意味着如果你删除一段代码,那么对该代码的引用将减少 1。

例如

git log -S"hello world" -- src/

这显示添加或删除 "hello world" 的提交。