From 6620e1347ca031f799207336ccff5b80ccb1d459 Mon Sep 17 00:00:00 2001 From: crazy-matt <46377983+crazy-matt@users.noreply.github.com> Date: Tue, 31 Aug 2021 22:20:18 +0100 Subject: [PATCH] fix the checkout branch... ...when a local branch exists and contains slashes in its name. --- forgit.plugin.zsh | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/forgit.plugin.zsh b/forgit.plugin.zsh index b693539..2dce324 100755 --- a/forgit.plugin.zsh +++ b/forgit.plugin.zsh @@ -229,8 +229,16 @@ forgit::checkout::branch() { " branch="$(eval "$cmd" | FZF_DEFAULT_OPTS="$opts" fzf --preview="$preview" | awk '{print $1}')" [[ -z "$branch" ]] && return 1 + # track the remote branch if possible - if ! git checkout --track "$branch" 2>/dev/null; then + if [[ "$branch" == "remotes/origin/"* ]]; then + if [[ -n $(git branch | grep -w "${branch#remotes/origin/}") ]]; then + # hack to force creating a new branch which tracks the remote if a local branch already exists + git checkout -b "track/${branch#remotes/origin/}" --track "$branch" + elif ! git checkout --track "$branch" 2>/dev/null; then + git checkout "$branch" + fi + else git checkout "$branch" fi }