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

Moving (renaming) submodules, recipe/script

From
W. Trevor King <wking@tremily.us>
Date
Jan 7, 2013, 00:36 UTC
Message-ID
<20130107003603.GA25698@odin.tremily.us>

Today I had to move my first submodule, and I discovered that Git's support for this is pretty limited. There have been a few patch series attempting to address this [1,2], but none of them seems to have pushed through into master (although I can't put my finger on a reason for why). There are also some SO postings discussing this [3,4]. It would be nice if `git mv` worked out of the box on submodules. Failing that, there could be a `git submodule mv` command that casts the appropriate spell. Failing that, there could be a recipe in Documentation/git-submodule.txt. Here's the best I could come up with for a `git-submodule-mv.sh`:

  #!/bin/sh
  # usage: git-submodule-mv.sh OLD NEW
  OLD=$(realpath --relative-to . "$1")
  NEW=$(realpath --relative-to . "$2")
  SHA=$(git ls-files -s "$OLD" | sed 's|^[0-9]* \([0-9a-f]*\) .*|\1|')
  NAME=$(git config -f .gitmodules --get-regexp 'submodule\..*\.path' "$OLD" |
    sed -e 's|^submodule.||' -e "s|.path $OLD\$||")
  GITDIR=$(realpath --relative-to "$NEW" .git/modules/"$NAME")
  git config -f .gitmodules submodule."$NAME".path "$NEW"
  git config -f .git/modules/"$NAME"/config core.worktree "../../../$NEW"
  git rm --cached "$OLD"
  mv "$OLD" "$NEW"
  echo "gitdir: $GITDIR" > "$NEW/.git"
  git update-index --add --cacheinfo 160000 "$SHA" "$NEW"

This only works from the repository root directory, and I'm sure makes a number of poor assumptions (e.g. old-style submodules that don't use `gitdir` links are not supported). It does work for some simple test cases. The tricky parts (e.g. path -> name conversion) are already worked out more robustly git-submodule.sh, so adding a new cmd_mv shouldn't be very difficult.

Could something like this live somewhere in Git, or are we waiting for a more integrated solution?

Cheers, Trevor

[1]: http://thread.gmane.org/gmane.comp.version-control.git/88720 [2]: http://thread.gmane.org/gmane.comp.version-control.git/143250 [4]: http://stackoverflow.com/questions/4323558/moving-submodules-with-git [3]: http://stackoverflow.com/questions/4604486/how-do-i-move-an-existing-git-submodule-within-a-git-repository

-- 
This email may be signed or encrypted with GnuPG (http://www.gnupg.org).
For more information, see http://en.wikipedia.org/wiki/Pretty_Good_Privacy
Next: Jonathan Nieder
Message 1 of 11 in “Moving (renaming) submodules, recipe/script”
  1. W. Trevor KingJan 7, 2013
  2. Jonathan NiederJan 7, 2013
  3. Jens LehmannJan 7, 2013
  4. Junio C HamanoJan 7, 2013
  5. Jens LehmannJan 7, 2013
  6. Junio C HamanoJan 7, 2013
  7. W. Trevor KingJan 7, 2013
  8. W. Trevor KingJan 8, 2013
  9. W. Trevor KingJan 8, 2013
  10. Jens LehmannJan 8, 2013
  11. W. Trevor KingJan 8, 2013

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.