Worktrees, hooks and automation
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-hotfixA 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.
#!/usr/bin/env bash
set -e
npm run lint
npm testchmod +x .git/hooks/pre-commitCarefulHooks 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.