Why doesn't "git log -‌- foo" work for deleted file foo?

GitGit Log

Git Problem Overview


My repository underwent changes such as:

  1. ...some unrelated commits...
  2. Commit new file foo with 100 lines of content
  3. ...intervening commits, some of which touch foo...
  4. Insert the contents of foo at the top of an existing file bar and git rm foo in the same commit
  5. ...more unrelated commits...

Now I want to see the log of deleted file foo. Everything I've read, including on SO, says I should be able to git log -- foo, but that command produces no output.

If I find the commit that includes deleting foo I can git log 1234abcd -- foo and see its log, so I think my path to foo isn't the problem. Also note that git merge-base HEAD 1234abcd outputs 1234abcd[...], so I think that should prove the commit is reachable from HEAD. Note that there is no file foo in my working tree (obvious, since it was deleted). Using Git 1.7.1.1 on OS X.

Why doesn't git log -- foo work for me and how can I fix it? Thanks!

Git Solutions


Solution 1 - Git

You want to use the --follow option on git log, which is described in the man page as:

Continue listing the history of a file beyond renames.

Effectively, not only does this allow you to see the history of a renamed file, but this also allows you to view the history of a file no longer in the working tree. So the command you should use should look something like:

git log --follow -- foo

Update:

Git 2.9+ has now enabled this by default for all git diff and git log commands:

> The end-user facing Porcelain level commands in the "git diff" and "git log" family by default enable the rename detection; you can still use "diff.renames" configuration variable to disable this.

Thanks to x-yuri for the heads up!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser385804View Question on Stackoverflow
Solution 1 - GitNathan KleynView Answer on Stackoverflow