threads / discuss / 53541

recurse-submodules can remove local changes when switching branches?

Subject: recurse-submodules can remove local changes when switching branches?

## tl;dr

3 messages between May 25, 2020 and May 25, 2020.

replies: 2people: 2as markdown or json

Laurence Tratt· May 25, 2020, 09:40 UTC · lore

If one creates a new repository, makes a new branch, adds a submodule, makes local changes to that submodule, then switches to another branch, it seems that git can silently remove those local changes.

Here's an example (the repositories involved are irrelevant other than they're small!):

  $ git clone https://github.com/ltratt/supuner/
  $ cd supuner
  $ git submodule add https://github.com/ltratt/extsmail extsmail
  $ git checkout --recurse-submodules -b b
  $ git commit -m "add submodule" .

At this point make a local change to extsmail/README.md and confirm that git has noticed that something in the submodule has changed:

  $ git status
  On branch b
  Changes not staged for commit:
    (use "git add <file>..." to update what will be committed)
    (use "git restore <file>..." to discard changes in working directory)
    (commit or discard the untracked or modified content in submodules)
          modified:   extsmail (modified content)
  no changes added to commit (use "git add" and/or "git commit -a")
then change branch (in the supuner repository, not extsmail!):
  $ git checkout --recurse-submodules master

The switch of branch succeeds without warning me that I have changes in my submodule. But then:

  $ git status
  On branch master
  Your branch is up to date with 'origin/master'.
  nothing to commit, working tree clean
My local changes to the submodule have disappeared entirely.
Switching back to `b` does not restore my changes:
  $ git checkout --recurse-submodules b
  On branch b
  nothing to commit, working tree clean

This happens on 2.26.2 and the current master. AFAICS this only happens because the master branch does not yet have the submodule registered in it. If it does, "checkout --recurse-submodules" maintains my local changes in the submodule across the branch switch.

Laurie
-- 
Personal                                             http://tratt.net/laurie/
Software Development Team                                http://soft-dev.org/
   https://github.com/ltratt              http://twitter.com/laurencetratt
Philippe Blain· May 25, 2020, 17:28 UTC · re: Laurence Tratt · lore

Re: recurse-submodules can remove local changes when switching branches?

Hi Laurence,
Show 42 quoted lines
> Le 25 mai 2020 à 05:40, Laurence Tratt <laurie@tratt.net> a écrit :
> 
> If one creates a new repository, makes a new branch, adds a submodule, makes
> local changes to that submodule, then switches to another branch, it seems
> that git can silently remove those local changes.
> 
> Here's an example (the repositories involved are irrelevant other than
> they're small!):
> 
>  $ git clone https://github.com/ltratt/supuner/
>  $ cd supuner
>  $ git submodule add https://github.com/ltratt/extsmail extsmail
>  $ git checkout --recurse-submodules -b b
>  $ git commit -m "add submodule" .
> 
> At this point make a local change to extsmail/README.md and confirm that
> git has noticed that something in the submodule has changed:
> 
>  $ git status
>  On branch b
>  Changes not staged for commit:
>    (use "git add <file>..." to update what will be committed)
>    (use "git restore <file>..." to discard changes in working directory)
>    (commit or discard the untracked or modified content in submodules)
>          modified:   extsmail (modified content)
> 
>  no changes added to commit (use "git add" and/or "git commit -a")
> 
> then change branch (in the supuner repository, not extsmail!):
> 
>  $ git checkout --recurse-submodules master
> 
> The switch of branch succeeds without warning me that I have changes in my
> submodule. But then:
> 
>  $ git status
>  On branch master
>  Your branch is up to date with 'origin/master'.
> 
>  nothing to commit, working tree clean
> 
> My local changes to the submodule have disappeared entirely.

I believe this is the same bug that was reported in 2018 here: https://lore.kernel.org/git/CAHsG2VT4YB_nf8PrEmrHwK-iY-AQo0VDcvXGVsf8cEYXws4nig@mail.gmail.com/

Show 11 quoted lines
> 
> Switching back to `b` does not restore my changes:
> 
>  $ git checkout --recurse-submodules b
>  On branch b
>  nothing to commit, working tree clean
> 
> This happens on 2.26.2 and the current master. AFAICS this only happens
> because the master branch does not yet have the submodule registered in it.
> If it does, "checkout --recurse-submodules" maintains my local changes in
> the submodule across the branch switch.
Thanks, this information should be useful for debugging.
Philippe.
Laurence Tratt· May 25, 2020, 22:09 UTC · re: Philippe Blain · lore

Re: recurse-submodules can remove local changes when switching branches?

On Mon, May 25, 2020 at 01:28:24PM -0400, Philippe Blain wrote:
Hello Philippe,
> I believe this is the same bug that was reported in 2018 here:
> https://lore.kernel.org/git/CAHsG2VT4YB_nf8PrEmrHwK-iY-AQo0VDcvXGVsf8cEYXws4nig@mail.gmail.com/
Yes, I agree!
Laurie

← back to recent threads