Search code examples
gitshellcommandrepo

How to get the commit id of all git repository heads inside a project?


Let's say the directory name ABC is my project which is not a git repository. Inside that dir ABC i have many directories which are git repository respectively. And i want every git repository's commit id of their heads with a single command (repo or git command) or script.

Thanks in advance.


Solution

  • In Bash,

    find ABC -name .git | awk '{
        cmd = "GIT_DIR="$NF" git rev-parse HEAD"
        while ( ( cmd | getline result ) > 0 ) {
            print $NF, result
        }
        close(cmd)
    }'
    

    The script iterates all .git under ABC, assuming that they are all git repositories, runs git rev-parse HEAD in each to get the head commit, and prints the repository folders and the SHA1 values.

    Reference: https://stackoverflow.com/a/55278818/6330106