{"thread":{"id":"56716","subject":"Git submodule update remote keeps using previous default branch","startedAt":"2021-10-15T08:35:03Z","lastAt":"2021-10-15T10:46:10Z","messageCount":3,"participants":["Sjoerd Langkemper","Bryan Turner"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"438813","messageId":"CAA1vfca+kPSsitsZad-bmrd+o1ay60NXZrH2zGLpwN69Px-rtw@mail.gmail.com","threadId":"56716","inReplyTo":null,"subject":"Git submodule update remote keeps using previous default branch","fromName":"Sjoerd Langkemper","fromEmail":"sjoerd-2021@linuxonly.nl","sentAt":"2021-10-15T08:34:49Z","receivedAt":"2021-10-15T08:35:03Z","isPatch":false,"sender":{"key":"sjoerd-2021@linuxonly.nl","avatar":null},"body":"I am having trouble with git 2.33.1 checking out the wrong branch for\nsubmodules for which the default branch has changed. `git submodule\nupdate --remote` seems to remember the branch name to retrieve, while\nI expect it to use the remote HEAD every time. This causes unexpected\nbehaviour when the remote HEAD starts pointing to another branch.\n\nI create a new git project and add a submodule, with `git submodule\nadd git@host:foo/testproject.git`.\n\nThis checks out the default branch, `master` in this case. `git remote\nshow origin` also shows that `master` is the HEAD branch. Running `git\nsubmodule update --remote` updates the submodule to the latest master.\n\nNow I change the default branch on the remote (using Gitlab's web\ninterface) to `newmaster`.\n\n`git remote show origin` now correctly shows `newmaster` as the\nremote's HEAD branch. However, running `git submodule update --remote`\nstill updates the submodule to the latest `master` branch, while I\nexpect it to update to the lastest `newmaster` branch.\n\nThere's no branch specified in .gitmodules or .git/config. I am not\nsure how git remembers the branch. When switching `testproject` to\n`newmaster` manually and then running `git submodule update --remote`,\nit is reset to `master` again.\n\nIs this a bug? Can I change the branch somehow?\n\nRegards,\n\nSjoerd Langkemper\n"},{"id":"438814","messageId":"CAGyf7-HZirxc0qoD1u9a3jx4egsjq=-Mdy_HCf2o-oWBrdUX+A@mail.gmail.com","threadId":"56716","inReplyTo":"CAA1vfca+kPSsitsZad-bmrd+o1ay60NXZrH2zGLpwN69Px-rtw@mail.gmail.com","subject":"Re: Git submodule update remote keeps using previous default branch","fromName":"Bryan Turner","fromEmail":"bturner@atlassian.com","sentAt":"2021-10-15T09:05:07Z","receivedAt":"2021-10-15T09:05:22Z","isPatch":false,"sender":{"key":"bturner@atlassian.com","avatar":"https://gravatar.com/avatar/16bcf3167981c1ef7c804e502642366d888a35b0d0b0a4ca01fdc442aa1acb1e?d=mp&s=160"},"body":"On Fri, Oct 15, 2021 at 1:35 AM Sjoerd Langkemper\n<sjoerd-2021@linuxonly.nl> wrote:\n>\n> I am having trouble with git 2.33.1 checking out the wrong branch for\n> submodules for which the default branch has changed. `git submodule\n> update --remote` seems to remember the branch name to retrieve, while\n> I expect it to use the remote HEAD every time. This causes unexpected\n> behaviour when the remote HEAD starts pointing to another branch.\n>\n> I create a new git project and add a submodule, with `git submodule\n> add git@host:foo/testproject.git`.\n>\n> This checks out the default branch, `master` in this case. `git remote\n> show origin` also shows that `master` is the HEAD branch. Running `git\n> submodule update --remote` updates the submodule to the latest master.\n>\n> Now I change the default branch on the remote (using Gitlab's web\n> interface) to `newmaster`.\n>\n> `git remote show origin` now correctly shows `newmaster` as the\n> remote's HEAD branch. However, running `git submodule update --remote`\n> still updates the submodule to the latest `master` branch, while I\n> expect it to update to the lastest `newmaster` branch.\n\nI suspect this is going to be the same answer I gave on the last\nthread about submodules[1]. Submodules in Git store the _commit ID_\nthey point to in the tree, but they do not store a _ref name_, nor do\nthey have some indicator for \"It was the default branch\". They choose\na ref by (essentially) looping over the refs from the remote (which\nare always sent in alphabetical order) and choosing the first one with\na matching SHA. If your \"newmaster\" is alphabetically after \"master\",\nand \"master\" still exists, and both refs point to the same commit (or\nonly \"master\" points to it), I suspect your submodule is going to\nchoose that.\n\n[1] https://lore.kernel.org/git/CAGyf7-FAHJOb6iQYqYNt0WSk+zUHUJ_FjrU1xis1bBQd9Z6KPQ@mail.gmail.com/\n\n>\n> There's no branch specified in .gitmodules or .git/config. I am not\n> sure how git remembers the branch. When switching `testproject` to\n> `newmaster` manually and then running `git submodule update --remote`,\n> it is reset to `master` again.\n>\n> Is this a bug? Can I change the branch somehow?\n\nIf you updated the submodule to reference a commit that was on\n\"newmaster\" and not present on \"master\", I think it'd change.\n\n>\n> Regards,\n>\n> Sjoerd Langkemper\n"},{"id":"438837","messageId":"CAA1vfcYSZWEXsDBD2_0o48EYbU9e9nKVoFdQ0M=fDAOMki3bng@mail.gmail.com","threadId":"56716","inReplyTo":"CAA1vfca+kPSsitsZad-bmrd+o1ay60NXZrH2zGLpwN69Px-rtw@mail.gmail.com","subject":"Re: Git submodule update remote keeps using previous default branch","fromName":"Sjoerd Langkemper","fromEmail":"sjoerd-2021@linuxonly.nl","sentAt":"2021-10-15T10:45:55Z","receivedAt":"2021-10-15T10:46:10Z","isPatch":false,"sender":{"key":"sjoerd-2021@linuxonly.nl","avatar":null},"body":"The submodule is updated according to where `origin/HEAD` points. The\n`origin/HEAD` ref of a repository is not automatically updated when\nthe upstream default branch changes. However, this can be forced with\n`git remote set-head origin -a`.\n\nI think it is a little bit surprising that `origin/HEAD` does not\nnecessarily point to the remote default branch. However, changing the\ndefault branch should be pretty rare, so I understand this is not\nchecked every time.\n\nRegards,\n\nSjoerd Langkemper\n\nOn Fri, Oct 15, 2021 at 10:34 AM Sjoerd Langkemper\n<sjoerd-2021@linuxonly.nl> wrote:\n>\n> I am having trouble with git 2.33.1 checking out the wrong branch for\n> submodules for which the default branch has changed. `git submodule\n> update --remote` seems to remember the branch name to retrieve, while\n> I expect it to use the remote HEAD every time. This causes unexpected\n> behaviour when the remote HEAD starts pointing to another branch.\n>\n> I create a new git project and add a submodule, with `git submodule\n> add git@host:foo/testproject.git`.\n>\n> This checks out the default branch, `master` in this case. `git remote\n> show origin` also shows that `master` is the HEAD branch. Running `git\n> submodule update --remote` updates the submodule to the latest master.\n>\n> Now I change the default branch on the remote (using Gitlab's web\n> interface) to `newmaster`.\n>\n> `git remote show origin` now correctly shows `newmaster` as the\n> remote's HEAD branch. However, running `git submodule update --remote`\n> still updates the submodule to the latest `master` branch, while I\n> expect it to update to the lastest `newmaster` branch.\n>\n> There's no branch specified in .gitmodules or .git/config. I am not\n> sure how git remembers the branch. When switching `testproject` to\n> `newmaster` manually and then running `git submodule update --remote`,\n> it is reset to `master` again.\n>\n> Is this a bug? Can I change the branch somehow?\n>\n> Regards,\n>\n> Sjoerd Langkemper\n"}]}