Search code examples
gitversion

How to get the version of Git programmatically?


As detailed in How can I find out what version of git I'm running? you can get the git version via

$ git version
git version 2.30.0

But it looks like this is meant to be consumed by humans, not programs. Is that correct? If so is there a "plumbing" git version command that can be used for numerical comparisons of the version?


Solution

  • I checked the source code. There is a comment about keeping the format of git version stable so it is safe to parse it.

    void get_version_info(struct strbuf *buf, int show_build_options)
    {
        /*
         * The format of this string should be kept stable for compatibility
         * with external projects that rely on the output of "git version".
         *
         * Always show the version, even if other options are given.
         */
        strbuf_addf(buf, "git version %s\n", git_version_string);
    ...