Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find commit when line was deleted/removed?

Tags:

git

git-log

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.

like image 593
matthewwithanm Avatar asked Sep 25 '12 21:09

matthewwithanm


People also ask

How do I see all commit history?

`git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first.

How do I see the commited changes?

If you have the hash for a commit, you can use the git show command to display the changes for that single commit. The output is identical to each individual commit when using git log -p .

How do I see when a change was last committed?

Find what file changed in a commit To find out which files changed in a given commit, use the git log --raw command. It's the fastest and simplest way to get insight into which files a commit affects.


2 Answers

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.

like image 144
lettertwo Avatar answered Sep 30 '22 13:09

lettertwo


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.

like image 24
James Avatar answered Sep 30 '22 12:09

James