gpt4 book ai didi

git - 搜索文件的所有先前 Git 提交,查找谁添加了(拼写错误的)字符串

转载 作者:太空狗 更新时间:2023-10-29 14:38:19 24 4
gpt4 key购买 nike

我想查看谁在何时对文件进行了更改。有一条用蹩脚的英语写的评论,没有注明出处。我想搜索(二进制搜索?)通过对该文件的所有提交以获取该评论的第一个实例。这甚至可以以自动化方式完成吗?

最佳答案

git blame filename | grep [string]

会做的

在 repo 中执行 git blame filename

例子:

$ git blame app/models/link.rb
^772df05 (Michael Durrant 2011-04-23 23:12:24 -0400 1) class Link < ActiveRecord::Base
5252167d (Michael Durrant 2012-09-03 21:39:48 -0400 2)
^772df05 (Michael Durrant 2011-04-23 23:12:24 -0400 3) belongs_to :group
^772df05 (Michael Durrant 2011-04-23 23:12:24 -0400 4) validates_presence_of :url_address
^772df05 (Michael Durrant 2011-04-23 23:12:24 -0400 5) validates_presence_of :group_id
...

所以要搜索一个字符串,只需 grep 即可:

$ git blame app/models/link.rb | grep the_dt
00000000 (Not Committed Yet 2014-08-08 22:29:15 -0400 20) def verified_date=(the_dt)
00000000 (Not Committed Yet 2014-08-08 22:29:15 -0400 21) verified_date=the_dt

如果你只想要最后一行(第一个例子),使用tail:

$ git blame app/models/link.rb | grep url | tail -1
00000000 (Not Committed Yet 2014-08-08 22:33:19 -0400 26) def verify_url

还有 git log -S,例如请注意,即使搜索大写字母也必须使用小写字母(如本例中的管道/管道)。

$ git log -Spipeline
commit c0fdeb8a603dd6f61e487ff4b5f7de4df4d43677
Author: Michael Durrant <m2@snap2web.com>
Date: Sun Feb 23 19:33:09 2014 -0500

Switch application to Asset Pipeline (many changes).

还有 git log -G 可以让你使用正则表达式。即使您在字符串搜索中只有空格,这也很有用,例如

$ git log -Gasset.*pipeline                                                                                
commit c0fdeb8a603dd6f61e487ff4b5f7de4df4d43677
Author: Michael Durrant <m2@snap2web.com>
Date: Sun Feb 23 19:33:09 2014 -0500

Switch application to Asset Pipeline (many changes).

关于git - 搜索文件的所有先前 Git 提交,查找谁添加了(拼写错误的)字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25214674/

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