Finding first and last commit for every subfolder in directory


I need to loop over every folder in a directory and find the user responsible for the first and last commit. Is there any smart way to do this in git bash? I tried looking into this with the subprocess module in Python, and using that to loop through the folders, but not sure that is a good approach

What I have tried is

  • git log -- path/to/folder: This solution just lists all commits to that subfolder. But I wish to filter only the first and last commit. I also wish to loop through all folders in the directory
  • The replies in this stackoverflow thread link: They didn’t seem to work for me (either printing nothing, or giving an error)


Assuming you are interested in the current branch only, you can get the first commit via Git Bash with

git rev-list HEAD -- path/to/folder | tail -1

and the last commit with

git rev-list HEAD -- path/to/folder | head -1

git rev-list is similar to git log, but it is a "plumbing" command. "Plumbing" commands are a bit less user-friendly than "porcelain" commands like git log, but they are guaranteed to behave consistently regardless of your personal settings whereas "porcelain" commands may have different output depending on your config. Because of this, it’s usually a good idea to use "plumbing" commands when writing scripts/programs.

git rev-list returns only the commit hash by default, but you can use --pretty/--format options similar to git log.

head and tail take a longer input—in this case, the entire list of commits for a path—and return only the first/last n lines, where n is whatever number you give as the parameter. git log and git rev-list show the most recent commit first, so you need tail to get the first commit and head to get the last.

You could also get the last commit using

git rev-list HEAD -1 -- path/to/folder

without piping to head. However, you cannot get the first commit using Git’s built-in commit-limiting options, because e.g.

git rev-list HEAD --reverse -1 -- path/to/folder

applies the -1 limiter first, returning only the last commit, before applying --reverse.

Finally, it’s worth noting that Git doesn’t truly track directories, only files. If you create a folder with no files in it, it’s not possible to commit that folder, and if you delete all the files within a folder, then as far as Git is concerned that folder doesn’t exist anymore. The upshot is: these commands will get you the first and last commits that touch any file within the directory (and its subdirectories) as opposed to the directory itself. This distinction may or may not be important for your scenario.

Answered By – Daniel Smith

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published