#!/bin/sh # SPDX-License-Identifier: MIT set -eu export TZ=UTC LC_ALL=C export GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_GLOBAL=/dev/null export GIT_AUTHOR_NAME='Example Author' export GIT_AUTHOR_EMAIL='author@example.invalid' export GIT_COMMITTER_NAME='Example Author' export GIT_COMMITTER_EMAIL='author@example.invalid' export GIT_AUTHOR_DATE='2026-10-25T12:00:00+00:00' export GIT_COMMITTER_DATE='2026-10-25T12:00:00+00:00' test "$(git version)" = 'git version 2.56.0' mkdir switch-example cd switch-example git init -q --template= --initial-branch=main --object-format=sha1 git config commit.gpgsign false git config core.autocrlf false printf 'base\n' > note.txt git add note.txt git commit -qm 'Base note' git branch same-point printf 'unfinished work\n' > note.txt before=$(git hash-object note.txt) git switch -q same-point test "$(git branch --show-current)" = same-point test "$(git hash-object note.txt)" = "$before" printf 'Existing branch at the same commit: edits carried across\n' git status --short git switch -qc new-work test "$(git branch --show-current)" = new-work test "$(git hash-object note.txt)" = "$before" printf 'New branch: edits remain available for its next commit\n' git add note.txt git commit -qm 'Keep unfinished work' git switch -q main printf 'different local edit\n' > note.txt before=$(git hash-object note.txt) head_before=$(git rev-parse HEAD) if git switch -q new-work 2>switch-refusal.txt; then printf 'Unexpected switch success\n' >&2 exit 1 fi test "$(git branch --show-current)" = main test "$(git rev-parse HEAD)" = "$head_before" test "$(git hash-object note.txt)" = "$before" printf 'Conflicting target: switch refused; branch and edits retained\n' cat switch-refusal.txt rm switch-refusal.txt git status --short