Search code examples
gitversion-controlmercurialdevopsdvcs

How to find commit hash(s) / developer(s), date etc for a "given line" change in a file


This may be an odd question, but I thought I should check here.

I have a source code file in Git/Hg(Mercurial) distributed version control system.

Let's say at line# 100, I see:

if [[ `echo "${var}" | grep "system"` ]]; then env="env_system"; else env="env_local"; fi

Is this possible to find in Git(or similar DVCS) - How can I find, what commit(s) / commit hash(s) / developer(s) were involved for a "given line" in a file.

How can find which Git hash(es), developer(s) with date - were involved with adding/changing the above line in the file during the life of this file?


Solution

  • Answer to the first part of the question (author of the last change of this line)

    Sounds like a job for git blame :

    git blame -L 100,100 path/to/file
    

    will also output the line itself after the information you wanted, but it might be good enough for whatever you intend to do?

    And for a quicker use, maybe make an alias :

    git config --global alias.bl '!f() { git blame -L $1,$1 $2; }; f'
    

    then

    git bl 100 path/to/file