gpt4 book ai didi

git - 如何使用 Git 查找一行(或一行的一部分)的第一次出现?

转载 作者:太空狗 更新时间:2023-10-29 14:17:59 25 4
gpt4 key购买 nike

假设我们在代码中看到一行:

a = foo.bar + xyz.abc + fn() * 3;

现在我们知道 foo.bar 是“错误”代码或导致某些问题的代码。 git blame 将显示该行由 Michael 提供。但是,Michael 可能没有 checkin 那部分代码。他可能只更改了 * 3 部分。那么在这种情况下,我们如何才能找到 repo 中第一次出现的 foo.bar,并找出它是谁写的或添加的?

我想到了用

git show HEAD:path/to/file.js | grep 'foo.bar'

然后

git show HEAD~1:path/to/file.js | grep 'foo.bar'
git show HEAD~2:path/to/file.js | grep 'foo.bar'

然后将 2 增加到 34 等,直到我看不到显示 foo 的那一行.bar.

但是如果数字可以很大,比如 120343 呢?有更好或更快的方法吗?

最佳答案

你可以使用 blame with commit sha 来继续更深层次的指责,例如:

git blame -L65,+2 -- application.py
# the commit sha in the output is 4dd5a167
# 4dd5a167 (Boris Serebrov 2015-09-08 21:33:20 +0300 65) except database.ItemNotFound:
# 74311d17 (Boris Serebrov 2015-11-12 11:16:50 +0200 66) # no node - log error and create it

# continue blaming from the 4dd5a167's parent (4dd5a167^)
git blame -L65,+2 4dd5a167^ -- application.py

此功能用于 vim-fugitive vim 插件,它通常非常有用。您可以执行 :Gblame 然后按 ~ 来“重新 blame ”更改。即使您不是 vim 用户,也值得尝试看看它是如何工作的。 Here很好地解释了 Fugitive 的行为。

关于git - 如何使用 Git 查找一行(或一行的一部分)的第一次出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36041476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com