appzasGamesToolsDevLearnCheat sheetsQuick calcsConvertersReferenceNetworkTimeCalculatorsCompareLLM prices

Worktrees, hooks and automation

Lesson 3 of 411 minAdvanced

Work on two branches at once and run checks automatically before each commit.

Worktrees

A worktree is an extra working folder attached to the same repository. You can check out another branch next to the current one, without stashing or cloning again.

git worktree add ../app-hotfix hotfix-1.2
cd ../app-hotfix            # work here, the other folder is untouched
git worktree list
git worktree remove ../app-hotfix

A branch can be checked out in only one worktree at a time.

Hooks

Hooks are scripts in .git/hooks that Git runs at certain points. A pre-commit hook runs before each commit and can stop it by exiting with a non-zero code.

.git/hooks/pre-commit
#!/usr/bin/env bash
set -e
npm run lint
npm test
chmod +x .git/hooks/pre-commit

CarefulHooks in .git/hooks are not part of the repository, so they are not shared. For a team, keep them in the repository and set git config core.hooksPath .githooks, or use a tool such as pre-commit or Husky. Hooks also never replace server-side checks: anyone can skip them with --no-verify.

Useful settings

git config --global pull.rebase true
git config --global rerere.enabled true   # remember how you resolved a conflict
git config --global alias.lg "log --oneline --graph --decorate"

Test yourself

Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.

1. What is a worktree for?
2. Why are .git/hooks not shared with the team by default?
Hintgit config core.hooksPath ...

Key terms