I'd like to create a bash script that allows me to run some other bash script on master branch while allowing me to continue to work in some other branch.
I'm trying to achieve this using git worktree, but am having trouble doing so. These are the commands I'm running:
git checkout other-branch
git worktree add ./temp master
cd temp
./some-script
Once I'm in the worktree, I'd like to run git hooks, but it's telling me that .git isn't a directory in the worktree. Why is that? And is it possible to access the "original" .git directory in the worktree?
Each linked working tree has a private sub-directory in the repository’s $GIT_DIR/worktrees directory. The private sub-directory’s name is usually the base name of the linked working tree’s path, possibly appended with a number to make it unique. For example, when $GIT_DIR=/path/main/.git the command git worktree add /path/other/test-next next creates the linked working tree in /path/other/test-next and also creates a $GIT_DIR/worktrees/test-next directory (or $GIT_DIR/worktrees/test-next1 if test-next is already taken).
Within a linked working tree, $GIT_DIR is set to point to this private directory (e.g. /path/main/.git/worktrees/test-next in the example) and $GIT_COMMON_DIR is set to point back to the main working tree’s $GIT_DIR (e.g. /path/main/.git). These settings are made in a .git file located at the top directory of the linked working tree.
That is, $GIT_DIR doesn't straightforwardly point to your usual .git anymore when you have the worktree checked out, and yes, you can access the main .git from $GIT_COMMON_DIR.
One simple way to access a file regardless of which branch is checked out is to use git-cat-file. This might not work for you if you need to run git hooks on it, or if the script itself needs access to other files from the branch, but otherwise it's probably simpler.
For example:
git cat-file -p master:myscript.sh | bash -
If 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