# git submodule add fails when using both --branch and --depth

3 messages from 2017-09-30 to 2017-10-02. Participants: Thadeus Fleming, Stefan Beller, Jonathan Nieder.
Thread: https://gitlist.dev/t/46864

## Thadeus Fleming, 2017-09-30 17:20

Subject: git submodule add fails when using both --branch and --depth
Message-ID: <CAA4Wa2sVddG_SS70Dy_womGLKW0ipAjsxR4g+yjMKscjErRjvQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAA4Wa2sVddG_SS70Dy_womGLKW0ipAjsxR4g%2ByjMKscjErRjvQ%40mail.gmail.com

```
I'm running git 2.14.2 on Ubuntu 16.04.

Compare the behavior of

> git clone --branch pu --depth 1 https://github.com/git/git git-pu

which clones only the latest commit of the pu branch and

> mkdir tmp && cd tmp && git init
> git submodule add --branch pu --depth 1 https://github.com/git/git \
  git-pu

which gives the error

fatal: 'origin/pu' is not a commit and a branch 'pu' cannot be created
from it
Unable to checkout submodule 'git-pu'

Investigating further, there is indeed only one commit in the local repo:

> cd git-pu
> git log --oneline | wc -l
1

But that commit is the head of master.

> git branch -a
* master                                 remotes/origin/master
  remotes/origin/HEAD -> origin/master

This appears to be because git-submodule--helper does not accept a
--branch option. Using the --depth N option causes it to only clone N
commits from the default branch, which generally do not include the
desired branch. Thus, the next step,

git checkout -f -q -B "$branch" "origin/$branch"

fails, and provides the rather confusing error message above.

I'd suggest that git-submodule--helper learn a --branch option
consistent with git clone, and if that is impossible, that
git submodule add rejects the simultaneous use of both the --branch and
--depth options.



--tjf

```

## Stefan Beller, 2017-10-02 19:55

Subject: Re: git submodule add fails when using both --branch and --depth
Message-ID: <CAGZ79kYt1rC5D=Gk8Ei1FmwF_zqK2AQgP_TQFHsV-NGHfVgOKA@mail.gmail.com>
URL: https://gitlist.dev/e/CAGZ79kYt1rC5D%3DGk8Ei1FmwF_zqK2AQgP_TQFHsV-NGHfVgOKA%40mail.gmail.com
In-Reply-To: <CAA4Wa2sVddG_SS70Dy_womGLKW0ipAjsxR4g+yjMKscjErRjvQ@mail.gmail.com>

```
On Sat, Sep 30, 2017 at 10:20 AM, Thadeus Fleming
<thadeus.j.fleming@gmail.com> wrote:
> I'm running git 2.14.2 on Ubuntu 16.04.
>
> Compare the behavior of
>
>> git clone --branch pu --depth 1 https://github.com/git/git git-pu
>
> which clones only the latest commit of the pu branch and
>
>> mkdir tmp && cd tmp && git init
>> git submodule add --branch pu --depth 1 https://github.com/git/git \
>   git-pu
>
> which gives the error
>
> fatal: 'origin/pu' is not a commit and a branch 'pu' cannot be created
> from it
> Unable to checkout submodule 'git-pu'
>
> Investigating further, there is indeed only one commit in the local repo:
>
>> cd git-pu
>> git log --oneline | wc -l
> 1
>
> But that commit is the head of master.
>
>> git branch -a
> * master                                 remotes/origin/master
>   remotes/origin/HEAD -> origin/master
>
> This appears to be because git-submodule--helper does not accept a
> --branch option. Using the --depth N option causes it to only clone N
> commits from the default branch, which generally do not include the
> desired branch. Thus, the next step,
>
> git checkout -f -q -B "$branch" "origin/$branch"
>
> fails, and provides the rather confusing error message above.
>
> I'd suggest that git-submodule--helper learn a --branch option
> consistent with git clone, and if that is impossible, that
> git submodule add rejects the simultaneous use of both the --branch and
> --depth options.

Adding the branch field to the submodule helper is a great idea.

>
>
>
> --tjf

```

## Jonathan Nieder, 2017-10-02 22:08

Subject: Re: git submodule add fails when using both --branch and --depth
Message-ID: <20171002220844.GQ19555@aiede.mtv.corp.google.com>
URL: https://gitlist.dev/e/20171002220844.GQ19555%40aiede.mtv.corp.google.com
In-Reply-To: <CAA4Wa2sVddG_SS70Dy_womGLKW0ipAjsxR4g+yjMKscjErRjvQ@mail.gmail.com>

```
Hi,

Thadeus Fleming wrote:

> I'm running git 2.14.2 on Ubuntu 16.04.
>
> Compare the behavior of
>
>> git clone --branch pu --depth 1 https://github.com/git/git git-pu
>
> which clones only the latest commit of the pu branch and

Yes.

>> mkdir tmp && cd tmp && git init
>> git submodule add --branch pu --depth 1 https://github.com/git/git \
>  git-pu
>
> which gives the error
>
> fatal: 'origin/pu' is not a commit and a branch 'pu' cannot be created from it
> Unable to checkout submodule 'git-pu'

As a side note (you are using "git submodule add --depth", not "git
submodule update --depth"), I suspect that "submodule update --depth"
may not always do what people expect.

With add --depth, I agree with your expectation and after your fix
everything should work fine.  But with update --depth, consider the
following sequence of steps:

 1. I create a repository "super" with submodule "sub" and publish
    both.

 2. I make a couple commits to "sub" and a commit to "super" making
    use of those changes and want to publish them.

 3. I use "git push --recurse-submodules" to publish my commits to
    "sub" and "super":

    a. First it pushes to "sub".

    b. Then it pushes to "super".

Between steps 3(a) and 3(b), a person can still "git clone
--recurse-submodules" the "super" repository.  The repository "super"
does not have my change yet and "sub" does, but that is not a problem,
since commands like "git checkout --recurse-submodules" and "git
submodule update" know to check out the commit *before* my change in
the submodule.

But between steps 3(a) and 3(b), "git submodule update --depth=1"
would not work.  It would fetch the submodule with depth 1 and then
try to check out a commit that is deeper in history.

So I think there's more thinking needed there.

That's all a tangent to your report about add --depth, though.

Thanks,
Jonathan

```
