diff --git a/contrib/src/main.mak b/contrib/src/main.mak
index b04f0017177647a47dff4eee8d5b614427ad2a5f..e77981f054c0f93baf233793601ff183fd91b18e 100644
--- a/contrib/src/main.mak
+++ b/contrib/src/main.mak
@@ -330,6 +330,7 @@ HOSTVARS := $(HOSTTOOLS) \
 # $1: The URL of the Git repository.
 # $2: The remote branch to clone.
 # $3: The git refspec to checkout.
+# $4: Set to anything to preserve the .git directory
 ifeq ($(DISABLE_CONTRIB_DOWNLOADS),TRUE)
 download_git = $(error Trying to clone $(1) but DISABLE_CONTRIB_DOWNLOADS is TRUE, aborting.)
 else
@@ -337,7 +338,7 @@ download_git = \
 	rm -Rf $(@:.tar.xz=) && \
 	$(GIT) clone $(2:%=--branch %) $(1) $(@:.tar.xz=) && \
 	(cd $(@:.tar.xz=) && $(GIT) checkout $(3:%= %)) && \
-	rm -Rf $(@:%.tar.xz=%)/.git && \
+	(test -z "$(4)" && rm -Rf $(@:%.tar.xz=%)/.git) || true && \
 	(cd $(dir $@) && \
 	tar cJ $(notdir $(@:.tar.xz=))) > $@ && \
 	rm -Rf $(@:.tar.xz=)
diff --git a/contrib/src/onnx/rules.mak b/contrib/src/onnx/rules.mak
index 513c9a453bb3a82dbaf31448a465e6443088e085..c631564f4c23d6d6ac2eb131229903eae0890357 100644
--- a/contrib/src/onnx/rules.mak
+++ b/contrib/src/onnx/rules.mak
@@ -3,7 +3,7 @@ ONNX_VERSION := v1.6.0
 ONNX_URL := https://github.com/microsoft/onnxruntime.git
 
 $(TARBALLS)/onnxruntime-$(ONNX_VERSION).tar.xz:
-	$(call download_git,$(ONNX_URL),master,$(ONNX_VERSION))
+	$(call download_git,$(ONNX_URL),master,$(ONNX_VERSION),preserve .git)
 
 .sum-onnx: onnxruntime-$(ONNX_VERSION).tar.xz
 	$(warning $@ not implemented)
@@ -34,5 +34,3 @@ endif
 	if [ ! -d "$(PREFIX)/include/onnxruntime" ] ; then ( mkdir $(PREFIX)/include/onnxruntime ) fi
 	cd $< && cp -r ./include/onnxruntime/core/* $(PREFIX)/include/onnxruntime/
 	touch $@
-
-