首页 > 解决方案 > How to list the original commits (time and author) per file of a directory with git?

问题描述

I want to use git to list me (in ls-style):

I'm looking for a output like this:

File           Original Author         First Added       Commit
----------------------------------------------------------------
a.yaml            tom                  01/01/2020        1a12121
b.yaml            felix                05/01/2020        ad12257
[...]

Is his somehow possible with git?

标签: bashgit

解决方案


You can use git log --pretty-format and --diff-filter=A (A for added):

#!/bin/bash

echo "File           Original Author         First Added       Commit"
echo "----------------------------------------------------------------"

if [[ $# -eq 0 ]]; then
    # No arguments, list all in current directory
    files=(*)
else
    # Only show those supplied on the command line
    files=("$@")
fi

for file in "${files[@]}"
do
    if [[ -f $file ]]; then
        msg=$(git log --diff-filter=A --pretty=format:'%al %as %h' "$file")
        printf "%-17s %-20s %-17s %s\n" "$file" $msg
    fi
done

Example:

File           Original Author         First Added       Commit
----------------------------------------------------------------
CMakeLists.txt    ted                  2020-06-24        41b5c92
LICENSE           ted                  2019-06-25        39d3ad5
README.md         ted                  2019-06-25        0bfa2a3
TODO              ted                  2020-01-13        aaeea10

--pretty-format supports a few date formats although I couldn't find your exact format and it also supports a lot of different versions for displaying the author name.


推荐阅读