How to git blame a directory

2020-06-30 11:38发布

问题:

I would like to know how to use git blame to know who created a single directory.

When I try:

git blame DIRECTORY_NAME

I get:

fatal: no such path DIRECTORY_NAME in HEAD

Incidentally, the directory is empty. Any suggestions?

回答1:

Try getting a log of just that directory, and use the -p option to see the exact changes that occurred.

$ git log -p <path to directory>

This still might not tell you exactly who created the directory, since git seems to focus more on file content, but you might get some helpful clues just from seeing the first time any content was added to it.



回答2:

I created a small function what loops over all files and makes a directory blame view similar to GitHub.

Output format is:

 FILENAME      COMMIT-HASH  Commit-DATE  AUTHOR  COMMIT-MESSAGE

looks like this

 myfile1    abceeee 2019-04-23 19:26  Radon8472  Added file example
 readme.md  abd0000 2019-04-24 19:30  Radon8472  Update Readme-File
blamedir() 
{ 
  FILE_W=35; 
  BLAME_FORMAT="%C(auto) %h %ad %C(dim white)%an %C(auto)%s"; 

  for f in $1*; 
  do 
    git log -n 1 --pretty=format:"$(printf "%-*s" $FILE_W "$f") $BLAME_FORMAT" -- $f; 
  done; 
}; 

usage-eamples:

  • blamedir similar like blamedir ./
  • blamedir DIRECTORY_NAME/

Feel free to change the display format by changing the Variable BLAME_FORMAT in the function.

I think it should be possible to set this function as git-alias too.



标签: git