gpt4 book ai didi

Git 历史 - 通过关键字查找丢失的行

转载 作者:IT王子 更新时间:2023-10-29 00:47:21 29 4
gpt4 key购买 nike

我的 Git 存储库中有一行包含单词“Foo”的数百次提交。

是否有任何方法可以在上次的位置找到它的修订号?

最佳答案

这可以通过 -S ( gitlog ) 选项来解决。

 git log -SFoo -- path_containing_change

(您甚至可以添加时间范围:--since=2009.1.1 --until=2010.1.1)

-S<string>

Look for differences that introduce or remove an instance of <string>.
Note that this is different than the string simply appearing in diff output; see the pickaxe entry in gitdiffcore(7) for more details.

diffcore-pickaxe

This transformation is used to find filepairs that represent changes that touch a specified string.
When diffcore-pickaxe is in use, it checks if there are filepairs whose "original" side has the specified string and whose "result" side does not.
Such a filepair represents "the string appeared in this changeset".
It also checks for the opposite case that loses the specified string.


2014 年更新:

从那时起,您可以(来自 nilbusanswer ):

git log -p --all -S 'search string'
git log -p --all -G 'match regular expression'

These log commands list commits that add or remove the given search string/regex, (generally) more recent first.
The -p (--patch) option causes the relevant diff to be shown where the pattern was added or removed, so you can see it in context.

Having found a relevant commit that adds the text you were looking for (eg. 8beeff00d), find the branches that contain the commit:

git branch -a --contains 8beeff00d

(我在“How to list branches that contain a given commit?”中引用了最后一条命令)

关于Git 历史 - 通过关键字查找丢失的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2839253/

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