I've moved a Git repository (containing several submodules) to another directory on the local disk. Any git command produces the error:
fatal: Not a git repository: <absolute path to .git/modules/*>
The error is derived from absolute paths to files encoded in various places[1]. Two questions:
Thanks!
I notice that with all submodules, the .git
file contains:
gitdir: <absolute path to repository>
The core.worktree
parameter within each submodule (.git/modules/*/config
) is set to the absolute path of that submodule.
I'm not looking for a shell command, rather a native Git method.
I'm not sure if absolute paths were created with an older version of Git, and newer that has since adopted relative paths.
Use git rev-parse --show-toplevel to get the absolute path to the current repository.
git directory is a configuration file for git. Use the terminal to display the . git directory with the command ls -a . The ls command lists the current directory contents and by default will not show hidden files.
Finally, GIT_SSH_COMMAND means Git 2.10+, so if your version of Git is too old, you will need to update it first. Follow this answer to receive notifications.
git submodule sync synchronizes all submodules while git submodule sync -- A synchronizes submodule "A" only. If --recursive is specified, this command will recurse into the registered submodules, and sync any nested submodules within.
I wrote a oneliner (bash and coreutils required) that changes any .git gitdir file into a relative path:
find -type f -name .git -exec bash -c 'f="{}"; cd $(dirname $f); echo "gitdir: $(realpath --relative-to=. $(cut -d" " -f2 .git))" > .git' \;
And if you don't like find here's one with git submodule:
git submodule foreach --recursive '[[ -f .git ]] && echo "gitdir: $(realpath --relative-to=. $(cut -d" " -f2 .git))" > .git'
@r3dey3 solution adapted for Windows:
git submodule foreach --recursive '[[ -f .git ]] && chmod +w .git && echo "gitdir: $(realpath --relative-to=. "$(cygpath -u "$(cut -d" " -f2 .git)")")" > .git'
Here I added:
chmod +w .git
to remove readonly attributecygpath -u
call to convert Windows path to Unix pathIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With