Git worktrees - Lesson 3 of 5
Integrate the change, not the directory
The worktree is a workspace. The branch and its commits are what you integrate. Decide whether the target needs the whole task branch or one selected commit, then verify in the target worktree.
First preserve the task
Inside the task worktree, make the change reviewable before touching the target branch:
cd ../repo-readme-refresh
git status --short
git add README.md
git commit -m "Refresh README setup instructions"
git log --oneline main..HEAD
The final command asks, in effect, which commits this task has that main does not. Read that range before choosing an integration method.
Choose deliberately
| Need | Use | Target worktree action |
|---|---|---|
| The complete task branch | Merge | git merge docs/readme-refresh |
| Only one known commit | Cherry-pick | git cherry-pick <commit> |
| Linear history required | Fast-forward-only merge | git merge --ff-only docs/readme-refresh |
| Task needs its base updated before review | Rebase under team policy | Rebase in the task worktree, then re-test. |
Do not rebase by reflex: rebase rewrites commits. Use it only when the repository's policy and collaborators make that safe.
Integrate in the target
cd ../repo
git switch main
git status --short
git pull --ff-only
git merge docs/readme-refresh
# Run the repository's relevant checks here.
git log --oneline --decorate -5
The target must be clean before you integrate. Resolve any conflict there, verify the resulting tree there, and only then mark the task complete.
Retrieval check
Primary reading
- git-merge and git-cherry-pick - review their conflict and continuation instructions before using them under time pressure.
Ask follow-up questions: Show the agent your target branch, task branch, and commit range if you are unsure whether merge or cherry-pick is the smaller, safer move.