Reputation: 75589
If I run git branch
, I will see output that looks like this:
+ branch1
* branch2
branch3
+ branch4
branch5
The output tells me visually which branches are checked out, but I would need to do string parsing to determine whether a given branch is checked out in any worktree.
In particular, I can do the following check:
checked_out_branches="$(git branch | awk 'NF > 1 {print $2}')"
for branch in $checked_out_branches; do
if [[ "$branch" = "$target_branch" ]]; then
>&2 echo "Target branch '${target_branch}' is already checked out."
exit 1
fi
done
However, git branch
is a porcelain command and I am not sure if the output is stable.
Is there a more direct way to ask git whether a particular branch is checked out in any worktree?
Upvotes: 0
Views: 35