FindGit — CMake 4.3.0 Documentation

# FindGit

Finds the Git distributed version control system:

    find_package(Git [<version>] [...])

## Imported Targets

This module provides the following Imported Targets when the `CMAKE_ROLE` is `PROJECT`:

`Git::Git`

  Added in version 3.14.

  Target that encapsulates Git command-line client executable. It can be used in `generator expressions`, and commands like `add_custom_target()` and `add_custom_command()`. This target is available only if Git is found.

## Result Variables

This module defines the following variables:

`Git_FOUND`

  Added in version 3.3.

  Boolean indicating whether (the requested version of) Git was found.

`Git_VERSION`

  Added in version 4.2.

  The version of Git found.

## Cache Variables

The following cache variables may also be set:

`GIT_EXECUTABLE`

  Path to the `git` command-line client executable.

## Deprecated Variables

The following variables are provided for backward compatibility:

`GIT_FOUND`

  Deprecated since version 4.2: Use `Git_FOUND`, which has the same value.

  Boolean indicating whether (the requested version of) Git was found.

`GIT_VERSION_STRING`

  Deprecated since version 4.2: Use `Git_VERSION`, which has the same value.

  The version of Git found.

## Examples

Finding Git and retrieving the latest commit from the project repository:

    find_package(Git)
    if(Git_FOUND)
      execute_process(
        COMMAND ${GIT_EXECUTABLE} --no-pager log -n 1 HEAD "--pretty=format:%h %s"
        OUTPUT_VARIABLE output
        RESULT_VARIABLE result
        ERROR_QUIET
        OUTPUT_STRIP_TRAILING_WHITESPACE
      )
      if(result EQUAL 0)
        message(STATUS "Last Git commit: ${output}")
      endif()
    endif()