Progress:
TIER 2 · MODULE 56· Intermediate

git sh-setup

Sourced by Git's shell-script subcommands.

🎯 What & why

Internal shell setup library sourced by Git's shell-built subcommands. Provides the standard helper functions every porcelain shell script depends on.

🧠 Mental model

A 'standard library' for git's .sh commands. Source it and you get say, die, cd_to_toplevel, require_work_tree, reflog action setting, GIT_DIR resolution, and all the boilerplate the C plumbing usually handles.

🛠️ Synopsis

. "$(git --exec-path)/git-sh-setup"

# Then call helpers like:
require_work_tree
cd_to_toplevel
set_reflog_action "myscript"
say "doing the thing"
die "fatal: cannot proceed"

🎚️ Switches & options

FlagWhat it does
(sourced) sayPrint to stdout unless GIT_QUIET is set
(sourced) diePrint to stderr and exit 128
(sourced) cd_to_toplevelchdir to the worktree root
(sourced) require_work_treeAbort if not inside a working tree
(sourced) require_clean_work_treeAbort on uncommitted changes (also takes 'rebase'/'merge' verb)
(sourced) set_reflog_actionSet GIT_REFLOG_ACTION so reflog entries name your script
(sourced) git_pagerPipe through the configured pager

💡 Use cases

🧪 Examples

Bootstrap a custom git subcommand
. "$(git --exec-path)/git-sh-setup"
Refuse to run with a dirty tree
require_clean_work_tree rebase 'commit or stash first'
Move to the worktree root
cd_to_toplevel
Tag your reflog entries
set_reflog_action 'git foo'

🎓 Recommendations

🪤 Common pitfalls

🔗 Related modules

📝 Quiz

Hit each option, then Check answers. Score is recorded; Next is always open.