git-如何确定在哪个提交中添加了特定代码?
我想找出我在以下提交中添加了以下代码:
if (getListView().getChildCount() == 0)
getActivity().findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
我该如何实现?
Harshal Kshatriya asked 2019-12-24T13:15:04Z
4个解决方案
55 votes
git log -S searchTerm
为您提供引入搜索词的提交。
54 votes
在文件上运行git blame
。 它将为您显示每行的提交ID,日期和时间以及提交者。 然后只需复制提交标识符即可在git log <commit>
或git show <commit>
中使用它。
例如,我有一个名为test.txt的文件,在不同的提交中添加了以下行:
$ cat test.txt
First line.
Second line.
运行git blame
:
$ git blame test.txt
^410c3dd (Leigh 2013-11-09 12:00:00 1) First line.
2365eb7d (Leigh 2013-11-09 12:00:10 2) Second line.
第一位是提交ID,然后是名称,然后是日期,时间,时区,最后是行号和行内容。
40 votes
比在整个文件上发怪要快得多。 如果该行是${lineno}
,文件是${filename}
,则可以:
git blame -L ${lineno},${lineno} ${filename}
例:
git blame -L 2,2 test.txt
10 votes
git log -S "mention here line of code" [file-path]
例如:
git log -S "First line" test.txt
提供文件名及其路径是显而易见的,因为在大多数情况下,我们想知道是谁在特定文件中引入了特定代码段。