Reputation: 96586
Is there a way to determine the "main" worktree (i.e. the one with a .git
directory) by running a command in any worktree? This does not work:
git rev-parse --show-toplevel
The best I can think of is parsing git worktree list --porcelain
, and then testing if there is a .git
directory in each one. That seems a bit naff though. Does Git have a built in command to do this?
Upvotes: 3
Views: 1368
Reputation: 4641
Here is a one-liner that expands on @lukas-hankeln's answer. It should work for most common cases.
git rev-parse --path-format=absolute --git-common-dir | sed 's|\(.*\)/\.git|\1|'
Upvotes: 0
Reputation: 122
I dont know of a command, doing what you want, but you could parse the contents of the ".git"-File in your Worktree
Upvotes: 0
Reputation: 96586
Aha! It seems you can do
git rev-parse --path-format=absolute --git-common-dir
and then either just remove the .git
from the path (probably safe) or do
git rev-parse --path-format=absolute --show-toplevel
in the directory returned from the first path.
--path-format=absolute
is helpful because otherwise these commands sometimes return relative paths and sometimes absolute paths.
The comments on this answer helped me figure this out.
Upvotes: 6