diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8144e1328..0ab59f18f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -41,19 +41,22 @@ list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules")
 ################################################################################
 include(${PROJECT_SOURCE_DIR}/cmake/git_utils.cmake)
 macro(disable_git_describe)
-  message(WARNING "Disabling Z3_INCLUDE_GIT_DESCRIBE")
-  set(Z3_INCLUDE_GIT_DESCRIBE OFF CACHE BOOL "Include git describe output in version output" FORCE)
+  if(Z3_INCLUDE_GIT_DESCRIBE)
+    message(WARNING "Disabling Z3_INCLUDE_GIT_DESCRIBE")
+    set(Z3_INCLUDE_GIT_DESCRIBE OFF CACHE BOOL "Include git describe output in version output" FORCE)
+  endif()
 endmacro()
 macro(disable_git_hash)
-  message(WARNING "Disabling Z3_INCLUDE_GIT_HASH")
-  set(Z3_INCLUDE_GIT_HASH OFF CACHE BOOL "Include git hash in version output" FORCE)
-  unset(Z3GITHASH) # Used in configure_file()
+  if(Z3_INCLUDE_GIT_HASH)
+    message(WARNING "Disabling Z3_INCLUDE_GIT_HASH")
+    set(Z3_INCLUDE_GIT_HASH OFF CACHE BOOL "Include git hash in version output" FORCE)
+  endif()
 endmacro()
 option(Z3_INCLUDE_GIT_HASH "Include git hash in version output" ON)
 option(Z3_INCLUDE_GIT_DESCRIBE "Include git describe output in version output" ON)
 
 set(GIT_DIR "${PROJECT_SOURCE_DIR}/.git")
-if (EXISTS "${GIT_DIR}")
+if ((Z3_INCLUDE_GIT_HASH OR Z3_INCLUDE_GIT_HASH) AND EXISTS "${GIT_DIR}")
   # Try to make CMake configure depend on the current git HEAD so that
   # a re-configure is triggered when the HEAD changes.
   add_git_dir_dependency("${GIT_DIR}" ADD_GIT_DEP_SUCCESS)
@@ -63,13 +66,13 @@ if (EXISTS "${GIT_DIR}")
       if (NOT Z3GITHASH)
         message(WARNING "Failed to get Git hash")
         disable_git_hash()
+      else()
+        message(STATUS "Using Git hash in version output: ${Z3GITHASH}")
+        # This mimics the behaviour of the old build system.
+        set(Z3_FULL_VERSION_STR "${Z3_FULL_VERSION_STR} ${Z3GITHASH}")
       endif()
-      message(STATUS "Using Git hash in version output: ${Z3GITHASH}")
-      # This mimics the behaviour of the old build system.
-      set(Z3_FULL_VERSION_STR "${Z3_FULL_VERSION_STR} ${Z3GITHASH}")
     else()
       message(STATUS "Not using Git hash in version output")
-      unset(Z3GITHASH) # Used in configure_file()
     endif()
     if (Z3_INCLUDE_GIT_DESCRIBE)
       get_git_head_describe("${GIT_DIR}" Z3_GIT_DESCRIPTION)
@@ -93,6 +96,9 @@ else()
   disable_git_describe()
   disable_git_hash()
 endif()
+if(NOT Z3_INCLUDE_GIT_HASH)
+  unset(Z3GITHASH) # Used in configure_file()
+endif()
 
 ################################################################################
 # Useful CMake functions/Macros