45

How can I list the author names of a particular folder that's versioned by git?

I see that I can git blame a file and list the author of each line, but I can't do that for a folder, and I would also like only a unique list (each author listed only once)

JuJoDi
  • 14,627
  • 23
  • 80
  • 126
  • 1
    I think you should write a shell script to do that, with for-loop, pipe, grep, sort, uniq, etc. Also, git blame won't show all the authors but just the last-modifiers, I think you should do that with git log. – nicky_zs Jul 17 '14 at 15:45

3 Answers3

100

Actually, there is a native Git command for that, git shortlog:

git shortlog -n -s -- myfolder

The options do:

  • Option -s just shows the number of commits per contributor. Without it, the command lists the individual commits per author.
  • Option -n sorts the authors by number of commits (descending order) instead of alphabetical.

What is -- good for?

And just in case you have not encountered a loose -- in a git command yet: it is a separator option to mark that what follows cannot be a <revspec> (range of commits), but only a <pathspec> (file and folder names). That means: if you would omit the -- and by accident had a branch or tag named myfolder, the command git shortlog -n -s myfolder would not filter for the directory myfolder, but instead filter for history of branch or tag "myfolder". This separator is therefore useful (and necessary) in a number of git commands, like log or checkout, whenever you want to be clear whether what you specify is either a revision (commmit, branch, tag) or a path (folder or file name). And of course, this site already has a question on this.

ojdo
  • 8,280
  • 5
  • 37
  • 60
  • 2
    What does `--` do? – Aaron Franke Dec 07 '19 at 04:46
  • 6
    `git shortlog` accepts a `` argument. If `myfolder` by accident would be the name of a tag or branch, the shortlog would be limited to that label's commit history. The separator `--` is used by many git commands to specify that what follows may only ever be a file or directory name (a path specification), no command or revision. – ojdo Dec 09 '19 at 08:21
  • 4
    @AaronFranke thanks for asking; now I also incorporated the `--` part directly in the answer and linked to the canonical question on SO. – ojdo Mar 30 '20 at 07:58
26

Based on The shortest possible output from git log containing author and date

do this

git log --pretty=format:"%an%x09" myfolder | sort | uniq
rethab
  • 7,170
  • 29
  • 46
2

If you want only the list of authors of a repository:

git ls-files | xargs -n1 git blame --line-porcelain | sed -n 's/^author //p' | sort -d | uniq

If you want to do it for a file order by the number of lines of code contributions it is simple:

git blame --line-porcelain "_MY_FILE_" | sed -n 's/author //p' | sort | uniq -c | sort -rn

If you want to do it for the whole repository also ordered by code contributions:

git ls-files | xargs -n1 git blame --line-porcelain | sed -n 's/^author //p' | sort -f | uniq -ic | sort -nr