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.