merlin2011
merlin2011

Reputation: 75589

Is there a git native check whether a specific branch is checked out in any worktree?

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

Answers (0)

Related Questions