How to find commit when line was deleted/removed?

34,126

Solution 1

git log -c -S'missingtext' /path/to/file

git log doesn't show a diff for merge commits by default. Try the -c or --cc flags.

More discussion/explanation:
https://git-scm.com/docs/git-log
nabble.com

From the git-log docs:

-c With this option, diff output for a merge commit shows the differences from each of the parents to the merge result simultaneously instead of showing pairwise diff between a parent and the result one at a time. Furthermore, it lists only files which were modified from all parents.

--cc This flag implies the -c option and further compresses the patch output by omitting uninteresting hunks whose contents in the parents have only two variants and the merge result picks one of them without modification.

Solution 2

There is a great answer to this on Super User: Git: How do I find which commit deleted a line?

git blame --reverse START.. file.ext

This will show, for each line, the last commit where the line was present - say hash 0123456789. The next commit to follow will be the one which removed it. Use git log and search for hash 0123456789 and then its successor commit.

Solution 3

Quick and dirty way #2 - use a for loop.

for commit in $(git log --pretty='%H'); do
    git diff -U0 --ignore-space-change "$commit^" "$commit" | grep '^-.*missingtext' > /dev/null && echo "$commit"
done

This will include all merge changes because it explicitly specifies the base commit for the diff. I came up with this because git log -c -S... was giving me a bunch of false-positives. Also, when I specified a filepath in the initial git log command, it skipped the commit I was looking for.

Since this may run for a while, you can specify -n on the git log command or put an && break at the end of the loop if you only need 1 result.

Share:
34,126

Related videos on Youtube

matthewwithanm
Author by

matthewwithanm

I'm a nerd. But I'm not a very good one.

Updated on February 05, 2021

Comments

  • matthewwithanm
    matthewwithanm over 1 year

    I have a deleted line in a file in my Git repository. I knew some of the missing text, and the file that it was in, so I used git log -S'missingtext' /path/to/file.

    However, the only thing that came back was the commit in which I added the line containing the missing text. The text wasn't present in HEAD, and the commit that added it was present in my branch, so I knew that one of the commits in my branch's history must have removed it, but it wasn't showing up.

    After some manual searching, it turned out that the line was removed accidentally while resolving a conflict for a merge. So I'm wondering:

    1. Is this the reason why pickaxe couldn't find the commit that deleted the line?
    2. How could I have found where "missingtext" was deleted without digging through the history manually?

    Any insight on #1 would be great (I assumed that git log -S would give me my answer), but my real question is #2 since I'd like to be able to avoid this in the future.

    • nneonneo
      nneonneo about 10 years
      git log -p and /missingtext while in less is a quick 'n' dirty way to do this.
    • hypehuman
      hypehuman over 6 years
      Possible duplicate of How do I "blame" a deleted line
  • Jonathan
    Jonathan about 6 years
    If you want to find when a line was removed from a deleted file, you can use git log -c -S'missingtext' -- /path/to/file.
  • Aryeh Beitz
    Aryeh Beitz over 5 years
    if you don't know which file contained the missing text, you can omit the /path/to/file and just run git log -c -S'missingtext'
  • SpeedOfSpin over 2 years
    On windows I had to do git log -c -S "missingtext" /path/to/file - Note the double quotes
  • Superole
    Superole almost 2 years
    "The next commit to follow will be the one which removed it." -This is not always true. The blame --reverse will show the latest commit - by timestamp - to contain a line, but it may have been removed by an earlier commit from a different branch.
  • Philip Rego
    Philip Rego 5 months
    This is showing commits that don't even contain 'missingtext' in /path/to/file
  • Philip Rego
    Philip Rego 5 months
    You give any way to search for 'missingtext'