Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - Find when a method is removed

Tags:

java

git

I am using Git to version control a large java project.

Is it possible to know at which commit a certain method is added or removed from a certain class?

like image 559
stderr Avatar asked Dec 12 '22 08:12

stderr


2 Answers

You can search for the name of the method and you will find the all commits that entered or deleted that string:

git log -c -S'methodName' /path/to/file.java

Another solution is to find the last commit in which that method exists:

$ git blame --reverse START_COMMIT.. file.ext

START_COMMIT is a commit in which you know for sure the method still exists. You will get a git blame output in which you can see the last commit in which that method existed, something like:

f590002e (user 2014-01-13 17:27:25 +0000 26)     public void save() {
f590002e (user 2014-01-13 17:27:25 +0000 27)         JPA.em().persist(this);
f590002e (user 2014-01-13 17:27:25 +0000 28)     }
like image 139
Atropo Avatar answered Dec 24 '22 19:12

Atropo


Try the "pickaxe" option of git log:

git log -S<your-method-name>

That will give you all the commits where the string is added or removed.
Note there is no space after the -S.

like image 43
elmart Avatar answered Dec 24 '22 21:12

elmart