gitgit-worktree

git worktree with relative path?


I have a master and a setup branch in my repo. I'm keeping the setup branch checked out as a worktree inside the main repo folder via

git worktree add ./local/setup
echo '/local' > .gitignore

So the main repo folder is on master, and the local/setup folder is on setup. Everything is fine and dandy, I can work on my setup files without having to switch branches, I can commit from within local/setup etc.

But if I try to move the entire repo, or access it from a different Linux boot (/home/myrepo becomes /mnt/ubu/home/myrepo), things break. The problem seems to be that git's worktree functionality records absolute paths, in

myrepo/.git/worktrees/setup/gitdir
myrepo/local/setup/.git

Can I convert these to relative paths to make the repo + embedded worktree relocatable? I'm not sure what the paths in those files should be relative to, but I can experiment. Is this setup dangerous?


Solution

  • I make a simple bash script for my personal use here:

    https://github.com/Kristian-Tan/git-worktree-relative

    Note that this answer is just a copy-paste from my README.md

    ...

    My solution

    Usage

    Installation

    Automatic Installation

    git clone https://github.com/Kristian-Tan/git-worktree-relative.git
    cd git-worktree-relative
    sudo bash install.sh
    

    Manual Installation

    Uninstallation

    ...

    Credits