How to automatically checkout git repository after FetchContent_MakeAvailable

I am using FetchContent_MakeAvailable in my project, and the code is:

include(FetchContent)
FetchContent_Declare(opencv
  GIT_REPOSITORY "https://github.com/opencv/opencv.git"
  GIT_TAG "origin/master"
  SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/opencvc"
)
FetchContent_MakeAvailable(opencv)

It can fetch opencv repository sucessfully. However, I need to manually checkout master branch.

Is it possible to automatically git checkout master by FetchContent_Declare or FetchContent_MakeAvailable.

The topic has been discussed before.
The module only guarantees that the right commit is checked-out. If you really need to check out a branch, you have to do it by yourself.

1 Like