<p>Following snippet is from kubernetes official documentation (<a href="http://kubernetes.io/docs/user-guide/volumes/#gitrepo" rel="nofollow">http://kubernetes.io/docs/user-guide/volumes/#gitrepo</a>):</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: server
spec:
  containers:
  - image: nginx
    name: nginx
    volumeMounts:
    - mountPath: /mypath
      name: git-volume
  volumes:
  - name: git-volume
    gitRepo:
      repository: "git@somewhere:me/my-git-repository.git"
      revision: "22f1d8406d464b0c0874075539c1f2e96c253775"
</code></pre>

<p>The above will mount complete git repo inside the container at /mypath. Is there a way to mount only specific sub-directory inside of the git repo inside the container at /mypath?</p>

<p>Yes there is a way to do it. See following example.
I have <a href="https://github.com/surajssd/hitcounter" rel="noreferrer">git repo</a> which has sub directory <a href="https://github.com/surajssd/hitcounter/tree/master/configs" rel="noreferrer"><code>configs</code></a>.</p>

<p>So here is pod file which I am using:</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: server
spec:
  containers:
  - image: nginx
    name: nginx
    volumeMounts:
    - mountPath: /mypath/
      name: git-volume
      subPath: "hitcounter/configs"
  volumes:
  - name: git-volume
    gitRepo:
      repository: "https://github.com/surajssd/hitcounter"
      revision: "9fd11822b822c94853b1c74ceb53adb8e1d2cfc8"
</code></pre>

<p>Note the field <code>subPath</code> in <code>containers</code> in <code>volumeMounts</code>. There you specify what sub-directory from the volume you want to mount at <code>/mypath</code> inside your container.</p>

<p>docs say:</p>

<pre><code>$ kubectl explain pod.spec.containers.volumeMounts.subPath
FIELD: subPath &lt;string&gt;

DESCRIPTION:
     Path within the volume from which the container's volume should be mounted.
     Defaults to "" (volume's root).
</code></pre>

<p><strong>OR</strong></p>

<p>create pod config file like this</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: server
spec:
  containers:
  - image: nginx
    name: nginx
    volumeMounts:
    - mountPath: /mypath/
      name: git-volume
      subPath: "configs"
  volumes:
  - name: git-volume
    gitRepo:
      repository: "https://github.com/surajssd/hitcounter"
      revision: "9fd11822b822c94853b1c74ceb53adb8e1d2cfc8"
      directory: "."
</code></pre>

<p>The difference here is I have specified <code>directory: "."</code> this ensures that mount point <code>/mypath</code> will become git repo and also the <code>subPath: "configs"</code> has changed since there is no extra directory <code>hitcounter</code>.</p>

<pre><code>$ kubectl explain pod.spec.volumes.gitRepo.directory
FIELD: directory &lt;string&gt;

DESCRIPTION:
     Target directory name. Must not contain or start with '..'.  If '.' is
     supplied, the volume directory will be the git repository.  Otherwise, if
     specified, the volume will contain the git repository in the subdirectory
     with the given name.
</code></pre>

<p>HTH</p>
