git 如何找出添加了特定代码的提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19875104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:16:30  来源:igfitidea点击:

How to find out in which commit a particular code was added?

gitgit-commit

提问by Harshal Kshatriya

I want to find out in which commit did I add the code given below:

我想找出我在哪个提交中添加了下面给出的代码:

if (getListView().getChildCount() == 0)
                getActivity().findViewById(android.R.id.empty).setVisibility(View.VISIBLE);

How do I achieve this?

我如何实现这一目标?

采纳答案by Leigh

Run git blameon the file. It'll show you the commit ID, the date and time, and who committed it- for each line. Then just copy out the commit identifier and you can use it in git log <commit>or git show <commit>.

git blame在文件上运行。它会向您显示每行的提交 ID、日期和时间以及提交者。然后只需复制提交标识符,您就可以在git log <commit>or 中使用它git show <commit>

For example, I've got a file, called test.txt, with lines added on different commits:

例如,我有一个名为 test.txt 的文件,在不同的提交中添加了几行:

$ cat test.txt
First line.
Second line.

Running the git blame:

运行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.

The first bit is the commit ID, then name, then date, time, time zone, and finally the line number and line contents.

第一位是提交 ID,然后是名称,然后是日期、时间、时区,最后是行号和行内容。

回答by Rahil Ahmad

git log -S searchTerm

gives you the commits in which the search term was introduced.

为您提供引入搜索词的提交。

回答by cforbish

There is something quicker than issuing a blame on the full file. If the line is ${lineno}and the file is ${filename}you can:

有比对完整文件发出责备更快的事情。如果行是${lineno}并且文件是,${filename}您可以:

git blame -L ${lineno},${lineno} ${filename}

Example:

例子:

git blame -L 2,2 test.txt

回答by anshul

git log -S "mention here line of code" [file-path]    

For example:

例如:

git log -S "First line" test.txt         

Providing the file name with its path is obvious because, most of the time, we want to know who introduced a particular code segment in a particular file.

提供文件名及其路径是显而易见的,因为大多数时候,我们想知道谁在特定文件中引入了特定代码段。