34

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?

mc9
  • 6,121
  • 13
  • 49
  • 87
  • Empty directories are not added to the repo in git, hence the message. Add something to the folder, commit out, then try again. – Mad Physicist Dec 14 '15 at 05:18
  • 1
    That reminds me of "tree blame" (RFC 2009: http://git.661346.n2.nabble.com/RFC-Tree-blame-git-blame-lt-directory-gt-td3218121.html) and git blame tree (2011: http://www.spinics.net/lists/git/msg152620.html). 2 RFCs which went nowhere. – VonC Dec 14 '15 at 07:50
  • 2
    Try [ git log --follow "directory" ] it will give you all the logs, even if the directory is renamed/deleted/modified. – love Dec 14 '15 at 08:56
  • @MadPhysicist Thanks for the suggestion, but the command returns the same message for non-empty directories. – mc9 Dec 15 '15 at 03:28

3 Answers3

28

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.

Jon Carter
  • 2,836
  • 1
  • 20
  • 26
6

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.

Radon8472
  • 4,285
  • 1
  • 33
  • 41
  • This is neat but I recommend adding --no-pager to the git command and adding an echo after, otherwise the experience is a bit bad. Somehow this git command's output doesnt even contain a newline. – Steven Lu Dec 05 '19 at 16:38
  • the --no-pager flag could be usefull. But I dont understand why you dont have newlines in the output, that is very strange – Radon8472 Dec 06 '19 at 14:17
  • 1
    Perhaps my git version takes `--pretty=format:` really literally... – Steven Lu Dec 06 '19 at 14:34
  • 1
    to make it work as `git blamedir ...` put the contents of function into the executable script file `git-blamedir` in any directory of PATH, i.e. `/usr/local/bin/git-blamedir`, remember about shebang and chmod+x – kyb May 08 '23 at 21:53
6

To git blame the current directory:

git ls-files -z | xargs -0n1 git blame

I adapted the above from here: https://gist.github.com/Alpha59/4e9cd6c65f7aa2711b79

A very very slow one-liner that goes through a git blame and sees how many lines were contributed by each author.

It seems I needed to press spacebar and Q at least once to fully scroll through the list.

Purplejacket
  • 1,808
  • 2
  • 25
  • 43
  • 1
    I had to add `--no-pager` to both commands to make it work faster `git --no-pager ls-files -z | xargs -0n1 git --no-pager blame` – Ivan -Oats- Storck Dec 13 '20 at 18:13
  • @Ivan this is a great refinement; but it points out an interesting feature/flaw with `ls-files`, namely that it recurses into subdirectories. So I tried running your command, `git --no-pager ls-files -z | xargs -0n1 git --no-pager blame`, on a big repo, and after a while MacOS Terminal got corrupted! The characters started looking like something out of the Matrix. Perhaps the solution could be adapted to not recurse into sub-directories using this: https://stackoverflow.com/questions/10452962/how-to-git-ls-files-for-just-one-directory-level – Purplejacket Dec 14 '20 at 01:58