# Clone Git Repository using new CMake FetchContent Functionality

**URL:** https://discourse.cmake.org/t/clone-git-repository-using-new-cmake-fetchcontent-functionality/4487
**Category:** Usage
**Created:** [November 15, 2021, 3:44pm UTC](https://discourse.cmake.org/t/clone-git-repository-using-new-cmake-fetchcontent-functionality/4487 "2021-11-15T15:44:01Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![JuliusCaesar](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@JuliusCaesar](https://discourse.cmake.org/u/JuliusCaesar)
#### Post date: [November 15, 2021, 3:44pm UTC](https://discourse.cmake.org/t/clone-git-repository-using-new-cmake-fetchcontent-functionality/4487/1 "2021-11-15T15:44:01Z")

</div>

Hey guys,  
I Read about the new cmake FetchContent .  
i’d like to use this to clone and checkout a git repository

```auto
include(FetchContent)

function(CloneRepository repositoryURL branchName projectName sourceDir)
  #Commands are left empty so that we only checkout the source and no not perform any kind of build
  message("Starting to clone ${projectName} into ${sourceDir}")
  FetchContent_Declare(
    "${projectName}"
    GIT_REPOSITORY "${repositoryURL}"
    SOURCE_DIR "${sourceDir}"
    GIT_TAG "origin/${branchName}"
    CONFIGURE_COMMAND ""
    BUILD_COMMAND ""
    INSTALL_COMMAND ""
  )
  FetchContent_MakeAvailable(${projectName})
endfunction(CloneRepository)

```

usage  
`CloneRepository("https://github.com/opencv/opencv.git" "master" "opencv" "${CMAKE_CURRENT_LIST_DIR}/../opencv")`

Now the problem is, I do not want to checkout a specific tag but the latest commit of a specified branch. When running this code the checkout git repo is much bigger than the manually cloned Repo. Also the Upstream Branch does not seam to be set.

Can you help me how to checkout the latest commit of a specified branch?

greets Julian

---

<div class="post-metadata">

### Author: ![buildSystemPerson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/buildsystemperson/32/2851_2.png) [@buildSystemPerson](https://discourse.cmake.org/u/buildSystemPerson)
#### Post date: [November 15, 2021, 5:15pm UTC](https://discourse.cmake.org/t/clone-git-repository-using-new-cmake-fetchcontent-functionality/4487/2 "2021-11-15T17:15:54Z")

</div>

From the [CMake docs](https://cmake.org/cmake/help/latest/module/ExternalProject.html?highlight=git_tag)

GIT\_TAG

Git branch name, tag or commit hash. Note that branch names and tags should generally be specified as remote names (i.e. origin/myBranch rather than simply myBranch). This ensures that if the remote end has its tag moved or branch rebased or history rewritten, the local clone will still be updated correctly. In general, however, specifying a commit hash should be preferred for a number of reasons:

If the local clone already has the commit corresponding to the hash, no git fetch needs to be performed to check for changes each time CMake is re-run. This can result in a significant speed up if many external projects are being used.

Using a specific git hash ensures that the main project’s own history is fully traceable to a specific point in the external project’s evolution. If a branch or tag name is used instead, then checking out a specific commit of the main project doesn’t necessarily pin the whole build to a specific point in the life of the external project. The lack of such deterministic behavior makes the main project lose traceability and repeatability.

If GIT\_SHALLOW is enabled then GIT\_TAG works only with branch names and tags. A commit hash is not allowed.

---

<div class="post-metadata">

### Author: ![JuliusCaesar](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@JuliusCaesar](https://discourse.cmake.org/u/JuliusCaesar)
#### Post date: [November 16, 2021, 8:17am UTC](https://discourse.cmake.org/t/clone-git-repository-using-new-cmake-fetchcontent-functionality/4487/3 "2021-11-16T08:17:38Z")

</div>

Hey, does the doc for ExternalProject apply to FetchContent as well? FetchContent docs do not mention git shallow.  
I tried  
`CloneRepository("https://github.com/opencv/opencv.git" "origin/master" "opencv" "${CMAKE_CURRENT_LIST_DIR}/../opencv")`

However to upstream connection does not seam to be set correctly. Git Bash shows this  
 ![grafik](https://discourse.cmake.org/uploads/default/original/2X/1/157c76f4fa139314aa7ede2eee973882b9ba810d.png)

=\>so despite writing origin/master the upstream branch connection does not seam to be set correctly.  
I’m am using latest CMake 3.21.4 generation was done using CMake GUI

(of course you are right using a proper tag is better traceable, but for development purposes I’d like to use the latest of our (internal) shared Lib 😉

---

<div class="post-metadata">

### Author: ![JuliusCaesar](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@JuliusCaesar](https://discourse.cmake.org/u/JuliusCaesar)
#### Post date: [November 16, 2021, 8:18am UTC](https://discourse.cmake.org/t/clone-git-repository-using-new-cmake-fetchcontent-functionality/4487/4 "2021-11-16T08:18:34Z")

</div>

for some reason I’m only allowed to add only 1 img per Post.  
So here is what i would expect  
 ![grafik](https://discourse.cmake.org/uploads/default/original/2X/4/459cee2c28b329f88d5b5acda653c82b0a8b32bf.png)

---

<div class="post-metadata">

### Author: ![buildSystemPerson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/buildsystemperson/32/2851_2.png) [@buildSystemPerson](https://discourse.cmake.org/u/buildSystemPerson)
#### Post date: [November 16, 2021, 1:54pm UTC](https://discourse.cmake.org/t/clone-git-repository-using-new-cmake-fetchcontent-functionality/4487/5 "2021-11-16T13:54:14Z")

</div>

> [@JuliusCaesar](#):
>
> Hey, does the doc for ExternalProject apply to FetchContent as well? FetchContent docs do not mention git shallow.

FetchContent uses ExternalProject “This module enables populating content at configure time via any method supported by the [`ExternalProject`](https://cmake.org/cmake/help/latest/module/ExternalProject.html#module:ExternalProject) module.”

---

<div class="post-metadata">

### Author: ![buildSystemPerson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/buildsystemperson/32/2851_2.png) [@buildSystemPerson](https://discourse.cmake.org/u/buildSystemPerson)
#### Post date: [November 16, 2021, 1:57pm UTC](https://discourse.cmake.org/t/clone-git-repository-using-new-cmake-fetchcontent-functionality/4487/6 "2021-11-16T13:57:20Z")

</div>

Here are some FetchContent tutorials:

[https://enccs.github.io/cmake-workshop/fetch-content/](https://enccs.github.io/cmake-workshop/fetch-content/)

> **[CMake FetchContent vs. ExternalProject](https://www.scivision.dev/cmake-fetchcontent-vs-external-project/)**
>
> Why use CMake FetchContent vs. ExternalProject

[https://eliasdaler.github.io/using-cmake/](https://eliasdaler.github.io/using-cmake/)

---

<div class="post-metadata">

### Author: ![JuliusCaesar](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@JuliusCaesar](https://discourse.cmake.org/u/JuliusCaesar)
#### Post date: [November 17, 2021, 11:54am UTC](https://discourse.cmake.org/t/clone-git-repository-using-new-cmake-fetchcontent-functionality/4487/7 "2021-11-17T11:54:01Z")

</div>

Hey I took the example from [Using CMake and managing dependencies · Elias Daler's blog](https://eliasdaler.github.io/using-cmake/)  
Just added the Source Dir to the example

```auto
cmake_minimum_required (VERSION 3.21.0)

project(parent)

include(FetchContent)
FetchContent_Declare(foobar
  GIT_REPOSITORY "https://github.com/opencv/opencv.git"
  GIT_TAG "origin/master" # it's much better to use a specific Git revision or Git tag for reproducibility
  SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../opencv"
)
FetchContent_MakeAvailable(foobar)

add_executable (Test "Test.cpp" "Test.h")
target_link_libraries(Test PRIVATE foobar_target)

```

even with this there is no connection to the upstream branch. Am I missing something here?

---

<div class="post-metadata">

### Author: ![buildSystemPerson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/buildsystemperson/32/2851_2.png) [@buildSystemPerson](https://discourse.cmake.org/u/buildSystemPerson)
#### Post date: [November 17, 2021, 6:13pm UTC](https://discourse.cmake.org/t/clone-git-repository-using-new-cmake-fetchcontent-functionality/4487/8 "2021-11-17T18:13:35Z")

</div>

> [@JuliusCaesar](#):
>
> even with this there is no connection to the upstream branch. Am I missing something here?

What do you mean? I copy pasted your code locally and it seemed to pull it down just fine.

The only issues I had was it failed to find python 2.7 on my machine.

```auto
  CMake's 'find_host_package(PythonInterp 2.7)' found wrong Python version:

```

 ![image](https://discourse.cmake.org/uploads/default/original/2X/c/c1fbc34e60cf6e73c636333fad1561ab47c3b4ce.png)

Here is my code for reference:

```auto
### Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved. ###
cmake_minimum_required(VERSION 3.21)

include(FetchContent)

project(TEST4)

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

FetchContent_MakeAvailable(opencv)

add_executable(foobar)

target_sources(foobar PRIVATE main.cpp)

```

---

<div class="post-metadata">

### Author: ![JuliusCaesar](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@JuliusCaesar](https://discourse.cmake.org/u/JuliusCaesar)
#### Post date: [November 18, 2021, 12:23pm UTC](https://discourse.cmake.org/t/clone-git-repository-using-new-cmake-fetchcontent-functionality/4487/9 "2021-11-18T12:23:06Z")

</div>

Hey, thx for your reply 🙂  
could you verfiy this  
Open the cloned opencv repo with a Git Gui and see if the checkout commit is actually attached to origin

 ![first](https://discourse.cmake.org/uploads/default/original/2X/d/d4eb23ad1c7df1bf80f857386428d8a2bdd5733a.png)

This is the result in my case.

However when I open a shell and type in git clone … I’ll get this.  
(see next post. I’m only allowed to post 1 image per post)

This is my expected result from performing a clone operation. Can you observe the same behaviour?

---

<div class="post-metadata">

### Author: ![JuliusCaesar](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@JuliusCaesar](https://discourse.cmake.org/u/JuliusCaesar)
#### Post date: [November 18, 2021, 12:23pm UTC](https://discourse.cmake.org/t/clone-git-repository-using-new-cmake-fetchcontent-functionality/4487/10 "2021-11-18T12:23:36Z")

</div>

![second](https://discourse.cmake.org/uploads/default/original/2X/1/1665ee02b0b456c6b88b62bff24bc1051a93664b.png)

=\>this is the expected behaviour

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [November 18, 2021, 2:25pm UTC](https://discourse.cmake.org/t/clone-git-repository-using-new-cmake-fetchcontent-functionality/4487/11 "2021-11-18T14:25:25Z")

</div>

If I’m not mistaken, CMake does check out specific commit, not branch. Why? Because it does not have to. If you need such functionality you probably have to call git commands by yourself.

---

<div class="post-metadata">

### Author: ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)
#### Post date: [November 18, 2021, 4:26pm UTC](https://discourse.cmake.org/t/clone-git-repository-using-new-cmake-fetchcontent-functionality/4487/12 "2021-11-18T16:26:37Z")

</div>

OpenCV seems to have `origin/4.x` set as default branch.  
So if you clone on command line without further arguments you’ll get `origin/4.x` and not `origin/master`.  
If you now simply checkout `origin/master` you end up in headless state, because there is no local master branch.  
Please verify with `git status` for each directory what you are really seeing.

---

<div class="post-metadata">

### Author: ![JuliusCaesar](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@JuliusCaesar](https://discourse.cmake.org/u/JuliusCaesar)
#### Post date: [November 22, 2021, 2:40pm UTC](https://discourse.cmake.org/t/clone-git-repository-using-new-cmake-fetchcontent-functionality/4487/14 "2021-11-22T14:40:57Z")

</div>

hey. Thx I was afraid you might say that.  
Than I will go back execute\_process() ☹

---

<div class="post-metadata">

### Author: ![JuliusCaesar](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@JuliusCaesar](https://discourse.cmake.org/u/JuliusCaesar)
#### Post date: [November 22, 2021, 2:43pm UTC](https://discourse.cmake.org/t/clone-git-repository-using-new-cmake-fetchcontent-functionality/4487/15 "2021-11-22T14:43:51Z")

</div>

yep it’s like you said. I end up in a headless state. I even tried to omit the GIT\_TAG “origin/master” to just checkout the default. But still the heads are detached ☹

---

<div class="post-metadata">

### Author: ![JuliusCaesar](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@JuliusCaesar](https://discourse.cmake.org/u/JuliusCaesar)
#### Post date: [November 22, 2021, 2:44pm UTC](https://discourse.cmake.org/t/clone-git-repository-using-new-cmake-fetchcontent-functionality/4487/16 "2021-11-22T14:44:34Z")

</div>

sorry is it just me or does this link not work any more? I get a 404 error

---

<div class="post-metadata">

### Author: ![buildSystemPerson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/buildsystemperson/32/2851_2.png) [@buildSystemPerson](https://discourse.cmake.org/u/buildSystemPerson)
#### Post date: [November 22, 2021, 3:53pm UTC](https://discourse.cmake.org/t/clone-git-repository-using-new-cmake-fetchcontent-functionality/4487/17 "2021-11-22T15:53:05Z")

</div>

Use my original link, @kadir’s link is broken

[https://eliasdaler.github.io/using-cmake/](https://eliasdaler.github.io/using-cmake/)

---

<div class="post-metadata">

### Author: ![JuliusCaesar](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@JuliusCaesar](https://discourse.cmake.org/u/JuliusCaesar)
#### Post date: [November 25, 2021, 1:33pm UTC](https://discourse.cmake.org/t/clone-git-repository-using-new-cmake-fetchcontent-functionality/4487/18 "2021-11-25T13:33:25Z")

</div>

sorry just to be clear. You all have a detached head when using my snippet correct?  
At least that’s what I’m encountering after several tries

Thx for your help so far 🙂
