mc9
mc9

Reputation: 6349

How to git blame a directory

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?

Upvotes: 40

Views: 19971

Answers (3)

Purplejacket
Purplejacket

Reputation: 2118

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.

Upvotes: 9

Radon8472
Radon8472

Reputation: 4951

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.

Upvotes: 6

Jon Carter
Jon Carter

Reputation: 3436

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.

Upvotes: 32

Related Questions