Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions zsh/functions.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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 --detach "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() {
Expand Down