git/list[1] front-page[2] threads[3] people[4] search[5] about
 

git worktree, submodule and force checkout/switch

From
FKFederico Kircheis <federico.kircheis@gmail.com>
Date
Jan 26, 2022, 16:37 UTC
Message-ID
<2a50d67e-364b-5927-801b-00f38179e904@gmail.com>
Hello to everyone,

I would like to report what I believe is a bug, or at least an inconsistent behavior when using submodules and worktrees.

Consider following test-case

---- # create 2 repositories with one commit mkdir repo1 && (cd repo1 && git init && git commit --allow-empty -m "repo1") mkdir repo2 && (cd repo2 && git init && git commit --allow-empty -m "repo2")

# add submodule, a couple of branches, and a worktree cd repo1 git submodule add ../repo2 && git commit -m "add submodule" git switch -c branch1 git switch -c branch2 git worktree add ../repo1.w --detach

# test switch in the worktree cd ../repo1.w # git switch works git switch --recurse-submodule branch1 git switch --recurse-submodule master #git submodule update # (1) cat .git cat .gitmodules cat repo2/.git # (2) git switch --force branch1 # (3)error if no submodule update ----

Notice that if one forgets to git submodule update (1) before git switch --force branch1, even when using --recurse-submodule, there is no submodule, as repo1.w/repo2/ is empty (2).

It is confusing/unexpected that git switch --force fails and creates a repo1.w/repo2/.git file pointing to the wrong location.

As comparison, when cloning a repository and forgetting to do "git submodule update", then "git switch --force branch1" works as expected:

---- # create 2 repositories with one commit mkdir repo1 && (cd repo1 && git init && git commit --allow-empty -m "repo1") mkdir repo2 && (cd repo2 && git init && git commit --allow-empty -m "repo2")

cd repo1 git submodule add ../repo2 && git commit -m "add submodule" git switch -c branch1 git switch -c branch2

cd .. git clone repo1 repo1.c cd repo1.c git switch branch2 git switch --force branch1 # works, event without git submodule update ----

Notice: In both cases "git switch" and "git checkout" behave the same. Also the parameter "--recurse-submodule" does not change anything.

Best
Federico
PS: I'm not subscribed to the mailing list (yet), so please keep me in CC.
Next: Glen Choo
Message 1 of 3 in “git worktree, submodule and force checkout/switch”
  1. Federico KircheisJan 26, 2022
  2. Glen ChooJan 26, 2022
  3. Elijah NewrenJan 27, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.