Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ensuring relative Git paths

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:

  • Is there a Git command for fixing these paths, i.e., making them relative?[2]
  • Is there a way to ensure that future Git repositories only use relative paths?[3]

Thanks!


Notes

  1. 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.

  2. I'm not looking for a shell command, rather a native Git method.

  3. I'm not sure if absolute paths were created with an older version of Git, and newer that has since adopted relative paths.

like image 539
Gingi Avatar asked Jun 08 '12 18:06

Gingi


People also ask

How do I create an absolute path in git?

Use git rev-parse --show-toplevel to get the absolute path to the current repository.

How do you display the path to the current directory git?

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.

What is Git_ssh_command?

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.

What does git submodule sync do?

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.


2 Answers

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'
like image 187
r3dey3 Avatar answered Oct 22 '22 21:10

r3dey3


@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 attribute
  • cygpath -u call to convert Windows path to Unix path
like image 38
Sergey Avatar answered Oct 22 '22 22:10

Sergey