A script and a branch are both smaller than they look

What is a script, and what is a branch? Two things you use by name. A script is text whose first line names the program that reads it. A branch is a name for one commit, and a worktree is a second folder to check one out in.

  1. 01The question
  2. 02A script is text another program reads
  3. 03The first line names its reader
  4. 04Where scripts came from
  5. 05A commit is a snapshot
  6. 06A branch is a name
  7. 07One folder, taken in turns
  8. 08A worktree is a second folder

01The question

Most of what sits in /usr/local/bin is compiled programs, files only a processor can read. Some of it isn't. Some files there are text you can read with cat, and they run all the same. Those are scripts. What makes a text file runnable, what reads it, and where did the idea come from?

The second question comes from coding agents, which are said to work "in their own worktree". People have always made a branch to build a feature on. So what is the difference between a branch and a worktree?

Both answers have the same shape. Each is a small thing that names the real one. A script's first line names the program that does the work. A branch is a name for one commit, and your folder shows whichever commit you last checked out. This note builds on Your shell finds a tool by searching folders in order, which covers how the shell finds a command by its name.

02A script is text another program reads

A is a text file of commands. The processor never runs it. Another program, an , reads it and carries out each command. A compiled program is , which the processor runs directly. That is why cat shows every line of a script and shows garbage for node.

The file command tells the two kinds apart. On a Mac, file $(which node) says Mach-O 64-bit executable arm64, and file $(which npm) says a /usr/bin/env node script text executable. Both sit on , and the shell treats them the same way.

03The first line names its reader

Two things make a text file runnable. The first is the execute bit, a permission on the file that says it may be run. chmod +x deploy.sh sets it. Without it the shell stops at permission denied. The second is the first line: #! and then a path, the . When a program is started, the looks at the file's first bytes. If they are machine code in a format it knows, it loads them. If they are #!/bin/bash, it starts /bin/bash instead and hands it the script's path. #!/usr/bin/env node starts env, which looks node up on PATH, so the script runs on whichever Node comes first.

Fig 01

The kernel starts /usr/bin/env, handing it node and this file's path. env finds node on PATH, and node reads the rest of the file.

Four files, each started from zsh. Pick one to see what the kernel makes of its first bytes.

A file with no first line is refused by the kernel with exec format error. Shells catch that and run the file as a shell script themselves: zsh hands it to /bin/sh, and Bash runs it on its own. So a bare list of commands works when you type its name in a terminal, and fails when any other program tries to start it.

Inside, a shell script holds what you would type: commands in order, variables (NAME="world", then $NAME), and if, for and while, which turn the list into a program. ~/.zshrc (Where PATH comes from) is a script with neither a first line nor the execute bit, because nothing starts it. zsh reads it in, line by line, every time a shell opens.

A script is text that names its reader. The kernel reads the first line, and the program named there reads the rest.

04Where scripts came from

Unix, at Bell Labs from 1969, was built from small programs that each did one thing and could be chained together. Its first shell, Ken Thompson's, read a line you typed and ran it. It could just as well read the lines from a file, so sh file ran a saved list of commands. Those were the first scripts on Unix, an idea Louis Pouzin had already tried on MIT's CTSS in the mid-1960s (A script, and what a Bot is built from), and they were made for the plainest reason there is: anything you type every day, you could type once.

  • 1979. Stephen Bourne's shell replaced Thompson's in the Seventh Edition of Unix, under the same name, sh. Thompson's had if and goto, but as separate programs. Bourne put variables and if, for and while inside the shell, and scripts became programs in their own right.
  • 1980. Dennis Ritchie changed the kernel so that a file starting with #! names its own interpreter. He thought the idea may have come from Berkeley, and most people first met it in BSD Unix.
  • After that, every interpreter came in through the same door: Perl, Python, Ruby, Node. A JavaScript file with #!/usr/bin/env node at the top and the execute bit set runs like any other command. That is how npm can be a script.

Scripts lasted because they are glue. You don't need a new compiled program to chain together a few tools that already exist. The scripts in a package.json share the name and the idea: each is one line, which npm run hands to /bin/sh.

A script is the commands you would type, kept in a file, with a first line saying who should read them.

05A commit is a snapshot

The branch question needs the same taking apart, starting from the bottom. A git repository is a folder with a hidden .git folder inside it. The folder holds the files you edit, and .git holds the history. That history is made of commits. A is a frozen record of every file you staged at one moment, plus who made it, when, a message, and the commit that came before it, its parent. Its name is a hash of all of that, like 15fcd2c…, so a commit can never change. Different contents would give a different name.

A snapshot isn't a copy, though. Git stores each distinct version of a file once, and a commit that leaves a file alone points at the same stored version as its parent. git cat-file -p HEAD prints the newest commit: a tree (the files), a parent, an author, a committer and the message. That is all a commit is.

06A branch is a name

A is a name that points at one commit. It isn't a copy and it isn't a folder. It is a file in .git/refs/heads/ holding the 40 characters of a commit's hash. git branch dark-mode makes one by writing that one small file.

Another small file, .git/HEAD, says which branch you are on: ref: refs/heads/main. Committing does two things. It writes a new commit whose parent is the one your branch names, then moves your branch to point at it. Only the branch HEAD names ever moves. When people say two branches have diverged, they mean exactly this: two names pointing at different commits, each with commits the other lacks.

Fig 02
myproject/shows 15fcd, “fix typo”, from main
Commit on one branch, switch, commit on the other. A dashed box is a name pointing at a commit; ink is the commit the folder shows.

A commit is the thing itself. A branch is a name for the newest commit on a line, and it moves when you commit.

07One folder, taken in turns

goes the other way, from the history into the folder. Git rewrites the files in your folder to match the commit a branch names, and points HEAD at that branch. git switch dark-mode does it. (git switch arrived in 2019 to take this job over from git checkout, which also restores files and was easy to misread.) Edits you haven't committed come with you if they don't clash with the new files. If switching would overwrite them, git refuses.

So with no worktree, there is one folder, and branches take turns with it. Make dark-mode and switch to it, and nothing on disk changes, because it starts on the same commit as main. Edit and commit, and you have edited the only folder there is. It was showing dark-mode, so dark-mode moves and main stays. The commits main points to sit untouched in .git, and git switch main writes them back into the folder, while your dark-mode work waits in .git until you switch back.

Without a worktree, your branches take turns with one folder. Checking out decides whose turn it is.

08A worktree is a second folder

One folder can show only one commit at a time. A is a second folder from the same repository, with a different branch checked out, so both are on disk at once. git worktree add ../myproject-dark dark-mode makes one. There is still only one history. In the new folder, .git is a file of one line pointing back into the first folder's .git.

Committing works the same way in either folder. It moves whichever branch is checked out in the folder you are in, so there's no such thing as committing "a worktree". Git also refuses to check out one branch in two worktrees at once, so two folders can never move the same name.

That is why coding agents are given a worktree each. An agent working on a branch in your folder would switch the folder under you, rewriting the files you have open. With a folder of its own, it can edit, install and break things while yours stays as it was. A new worktree only has the files git tracks, though, so each one needs its own node_modules and its own install.

Fig 03
  1. 1.git3 commits, every branch's
  2. 2Files in myproject/main's, checked out
  3. 3Your editNot committed
  4. 4The agentNot started

You're in myproject/ on main, halfway through an edit to header.tsx.

Step 1 of 3
You and an agent in one repository. Step through it in one shared folder, then with a worktree each.

The branch is which line of history. The worktree is where its files sit on disk.

Connected

Side threads

Questions that came up once the main idea began to hold, kept as they were asked. Each opens beside the paragraph it grew from.

Terms

Checked

What the chat had wrong or only half right, against sources. The note above already says the corrected thing.

  1. Compiled came first, by about a year: FORTRAN in 1957, then Lisp's interpreter in 1958.Interpreting came first. Short Code ran through an interpreter on UNIVAC I in 1950, and Backus's own Speedcoding (1953) was interpreted before he led FORTRAN.Wikipedia, Short Code
  2. The Bourne shell arrived in 1977.Bourne began it around 1975. It was released in 1979, as sh in the Seventh Edition of Unix.Wikipedia, Bourne shell
  3. sh on a Mac is the original Bourne shell, kept as a fallback.On a Mac, /bin/sh runs Bash in POSIX mode. /private/var/select/sh is a link to /bin/bash.A Mac running macOS 26, read on 23 October 2026
  4. npm run build is literally a script that calls next, webpack and tsc.It is one line in package.json's scripts, which npm runs with /bin/sh. The next it calls is itself a script, in node_modules/.bin.npm docs, npm run
  5. V8 has a simpler two-tier setup than JavaScriptCore, and optimises more aggressively, sooner.V8 has four tiers too: Ignition, Sparkplug (2021), Maglev (2023) and TurboFan.V8 blog, Maglev
  6. JavaScriptCore was called SquirrelFish, dating back to 2008.SquirrelFish was the 2008 rewrite of JavaScriptCore's interpreter. JavaScriptCore itself began in 2002, when Apple took KDE's KJS engine for Safari.WebKit blog, Announcing SquirrelFish
  7. Both of Node's layers are C++, end to end.V8 is C++. Round it, Node has C++ bindings, libuv for its event loop (in C), and much of its API in JavaScript, in lib/.Node.js source
  8. Bun does the job of tsc, along with node, npm, webpack and jest.Bun runs TypeScript by stripping the types, and checks none of them. Type checking still needs tsc.Bun docs, TypeScript
  9. A tmux session survives a laptop's sleep more reliably than a terminal tab.Sleep pauses every process alike, tmux's server included, and all of them carry on at wake. tmux protects against the window or the connection going away.tmux manual
  10. Terminal.app already has split panes, covering most of what tmux gives you locally.Terminal's split (Command-D) shows two views of the same shell. Separate shells side by side need tabs, windows, or another terminal app.O'Reilly, Mac OS X Lion Pocket Guide: Terminal tips
  11. Switching branches in a shared folder means your uncommitted edits could get stashed.Git never stashes on its own. Uncommitted edits come with you when they don't clash, and git refuses to switch when they would be overwritten.Git docs, git switch
  12. A commit takes a snapshot of your current files.It records what you have staged with git add. Changes you haven't added stay out of the commit.Git docs, git commit
  13. Bun's speed is concentrated almost entirely in its Zig layer, not the engine.Bun's own docs credit JavaScriptCore as well, for starting faster and using less memory.Bun docs, What is Bun?