#!/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' GIT_AUTHOR_EMAIL='author@example.invalid' export GIT_COMMITTER_NAME='Example Author' GIT_COMMITTER_EMAIL='author@example.invalid' export GIT_AUTHOR_DATE='2026-10-27T12:00:00+00:00' export GIT_COMMITTER_DATE='2026-10-27T12:00:00+00:00' test "$(git version)" = 'git version 2.56.0' mkdir stage-example cd stage-example git init --template= --bare -q --initial-branch=main origin.git git init --template= -q --initial-branch=main repo cd repo for n in $(seq 1 20); do printf 'line %s\n' "$n"; done > note.txt git add note.txt git commit -qm 'Base' git remote add origin ../origin.git git push -q -u origin main >/dev/null awk '{if (NR==2) print "first change"; else if (NR==19) print "second change"; else print}' note.txt > updated.txt mv updated.txt note.txt printf 'y\nn\n' | git add -p -- note.txt > ../patch-dialogue git show :note.txt > ../staged-copy grep -q '^first change$' ../staged-copy grep -q '^line 19$' ../staged-copy grep -q '^second change$' note.txt test "$(git diff --cached --numstat -- note.txt)" = "$(printf '1\t1\tnote.txt')" test "$(git diff --numstat -- note.txt)" = "$(printf '1\t1\tnote.txt')" printf 'First hunk staged; second hunk remains only in working file\n' git diff --cached -- note.txt | grep '^+first change$' git diff -- note.txt | grep '^+second change$'