0

Suppose that a .gitmodules file includes the following lines

[submodule "sub/x"]
    path = sub/x

...which resulted from a command of the form

git submodule add $url sub/x

After the fact (and after having added several commits, both to the superproject as well as the submodule), I realize that I should have run this instead:

git submodule add --name x $url sub/x

...so that the two lines in the .gitmodules file corresponding to the two quoted earlier would have been

[submodule "x"]
    path = sub/x

I want to fix the superproject so that the name of the submodule is x rather than sub/x, but the path remains sub/x1.

My immediate interest is in learning how to fix the history (e.g. through an interactive rebase) of the superproject, so that in the end the repo looks as if the second git submodule add command was the one used from the beginning. (FWIW, this repo has not yet been published, though I do intend to do so.) For future reference, however, I am also interested in learning how to do this without modifying the history, i.e. by adding one or more new commits to it.

I have tried to remove the submodule and add it again (based on the git submodule deinit recipe given here), but I quickly find myself in a mess. (I think that part of the problem is that, as I already mentioned, I have added several commits to the submodule, made corresponding updates to the superproject's submodule information, along the way.)

Therefore, I am hoping to find more direct guidance on how to do this renaming.


1 There is an earlier post whose title suggests that it is asking exactly the question I am asking here, but in fact that post is asking for not only how to rename the submodule, but also how to change its path. It turns out that most of the content in the answers to that question focus on the problem of changing the module's path.

kjo
  • 33,683
  • 52
  • 148
  • 265

1 Answers1

4

I think renaming sections in .git/config and .gitmodules is just fine:

git config                --rename-section submodule.sub/x submodule.x
git config -f .gitmodules --rename-section submodule.sub/x submodule.x
git add .gitmodules
git commit -m "Rename submodule.sub/x -> submodule.x"
phd
  • 82,685
  • 13
  • 120
  • 165
  • 1
    Note that after modifying the `.gitmodules` file, you should generally add and commit it. Committing the rename by itself seems sensible. – torek Sep 20 '20 at 01:35