From f1bb9bcc4a3a34c45dc5a5c88c3594e324b31117 Mon Sep 17 00:00:00 2001 From: David Price Date: Thu, 9 Apr 2026 16:51:10 -0500 Subject: [PATCH 1/2] Enhance git2main for better worktree and stash handling Refactor git2main function to handle multiple worktrees and improve stashing logic. --- zsh/functions.zsh | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/zsh/functions.zsh b/zsh/functions.zsh index c5c31ed..3796b49 100644 --- a/zsh/functions.zsh +++ b/zsh/functions.zsh @@ -23,25 +23,38 @@ git-default-branch() { } ###--- Switch to main safely ---### -git2main() { - local stashed=0 - local default_branch - default_branch=$(git-default-branch) - # Check if there are any changes to stash - if ! git diff-index --quiet HEAD -- 2>/dev/null || [ -n "$(git ls-files --others --exclude-standard)" ]; then - echo "Stashing uncommitted changes..." - git stash push -u -m "git2main auto-stash $(date +%Y-%m-%d\ %H:%M:%S)" - stashed=1 - fi +git2main() { + local stashed=0 + local default_branch + default_branch=$(git-default-branch) - git fetch --all --prune && git switch "$default_branch" && git pull + # Check if there are multiple worktrees + local worktree_count + worktree_count=$(git worktree list | wc -l) - # Apply stash if changes were stashed - if [ "$stashed" -eq 1 ] && [ $? -eq 0 ]; then - echo "Applying stashed changes..." - git stash pop - fi -} + # Check if there are any changes to stash + if ! git diff-index --quiet HEAD -- 2>/dev/null || [ -n "$(git ls-files --others --exclude-standard)" ]; then + echo "Stashing uncommitted changes..." + git stash push -u -m "git2main auto-stash $(date +%Y-%m-%d\ %H:%M:%S)" + stashed=1 + fi + + git fetch --all --prune + + # If multiple worktrees exist, checkout the origin SHA directly + if [ "$worktree_count" -gt 1 ]; then + echo "Multiple worktrees detected, checking out origin/$default_branch SHA..." + git checkout "origin/$default_branch" + else + git switch "$default_branch" && git pull + fi + + # Apply stash if changes were stashed and previous command succeeded + if [ "$stashed" -eq 1 ] && [ $? -eq 0 ]; then + echo "Applying stashed changes..." + git stash pop + fi +} ###--- Switch to new branch based on origin/main ---### git2new() { From 4d72ec0a62a30eef9d4a76f2fd411417b506ed85 Mon Sep 17 00:00:00 2001 From: David Price Date: Thu, 9 Apr 2026 16:55:44 -0500 Subject: [PATCH 2/2] fix the checkout --- zsh/functions.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zsh/functions.zsh b/zsh/functions.zsh index 3796b49..5f257c7 100644 --- a/zsh/functions.zsh +++ b/zsh/functions.zsh @@ -44,7 +44,7 @@ git2main() { # If multiple worktrees exist, checkout the origin SHA directly if [ "$worktree_count" -gt 1 ]; then echo "Multiple worktrees detected, checking out origin/$default_branch SHA..." - git checkout "origin/$default_branch" + git checkout --detach "origin/$default_branch" else git switch "$default_branch" && git pull fi