Mark Needham

Thoughts on Software Development

Git: Getting the history of a deleted file

with 5 comments

We recently wanted to get the Git history of a file which we knew existed but had now been deleted so we could find out what had happened to it.

Using a simple git log didn’t work:

git log deletedFile.txt
fatal: ambiguous argument 'deletedFile.txt': unknown revision or path not in the working tree.

We eventually came across Francois Marier’s blog post which points out that you need to use the following command instead:

git log -- deletedFile.txt

I’ve tried reading through the man page but I’m still not entirely sure what the distinction between using and not using it is supposed to be.

If someone could explain it that’d be cool…

Written by Mark Needham

October 4th, 2011 at 10:33 pm

Posted in Version Control

Tagged with

  • Lachlan O’Dea

    It’s because the log command, like many, can accept either a branch name, or a path to a file. It checks the known branches and tags, and the extant paths in your working directory to work out what you meant. When neither exists, git can’t tell what “deletedFile.txt” is supposed to refer to. Putting — removes the ambiguity, because everything after — is always treated as a file name, implicitly using the current HEAD revision in this case.

  • steve moyer

    the — separates the revision(or revision range) from the paths.  As slight clairification to Lachlan’s statement is that you can use both revisions and paths

  • Anonymous

    Just to elaborate slightly on Lachlan’s comment. Using “–” to indicate it’s a file you’re interested in is also useful when there’s ambiguity in the name; on more than one occasion I’ve had a branch or tag with the same name as a file or directory, and using “–” is required to differentiate between the two (as branches get higher priority than files).

  • AR Haskell

    try in terminal:

    touch — -rf # create an empty file with name -rf


    rm -rf # does not work

    rm — -rf does the trick

  • Jeff Schumacher

    If you’re in PowerShell, this won’t work. You have to double-quote your double-dash: 
    git log “–” deletedFile.txt